Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to return a pointer from a function 1

Status
Not open for further replies.

greadey

Technical User
Oct 25, 2001
231
GB
Hi all,

I'm trying to understand pointers and have written some small programs to investigate these beasties.

Can someone tell why this function won't compile?

int * plus3 (int *x) {

++(*x);
return (x);

}

Thanks Greadey

 
Won't compile as in...
Whats the error that you are getting. I tried it and worked just fine for me.

#include <stdio.h>

int* plus3 (int *x) {

++(*x);
printf (&quot;\n inside this &quot;);
return (x);
}

void main()
{
int x, *y;
printf (&quot;\n enter a value&quot;);
scanf (&quot;%ld&quot;, &x);
y = plus3(&x);
printf (&quot;\n value is %ld&quot;, *y);
}

Try this out.

Cheers.
KK IBM MQSeries Specialist
 
I tried your example and fixed mine (it was a typo DUH!).

Anyway it all works as expected but when gcc compiles it I get a warning that &quot;plus3 was implicitly declared to return an int&quot;.

Strange since I implicitly declared it to return a pointer to an int.

Is this gcc or is it generic?
 
Add a prototype for your function.

#define <blah>

int *plus3(int *);

int main(void) {
etc...
}

If your function is not prototyped gcc
complains about it.
 
Nice one, thanks alot - a valuable lesson learned. I knew gcc could be picky (mind you that's what it's for eh?). I also found that if I compiled my original prog with debugging symbols gcc didn't barf at that either.

Makes me wonder if debugging symbols not only increase your binary and allow for debugging but also debug simple things as well :))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top