Results 1 to 7 of 7
Hi there Linuxers,
I'm getting some information about C language and this session of a C book (follow the above link) is using a bad example for me. When I'm ...
- 07-15-2010 #1Just Joined!
- Join Date
- May 2010
- Posts
- 9
Pointer as function return doubt
Hi there Linuxers,
I'm getting some information about C language and this session of a C book (follow the above link) is using a bad example for me. When I'm trying this example of function returning a pointer, my compiler is stating a warning that I return a pointer to a local variable. I realized that it is error prone after all this variable may be override before the function has done his execution. And the author is fooling me saying that this example is "perfectly safe". I'm wrong? There is something that I don't got yet? Can someone help me?
Sorry but this site is preventing me to post the link of book cause I'm a newbie, so a need the hack it. Just strip out the question signs:
{LINK}
?w?w?w
?cs.cf.ac.uk?
?/Dave/?
?C/?
?node10.html#SECTION001020000000000000000?
{/LINK}
Thanks...
- 07-15-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Try this: Pointers
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-15-2010 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Well, a quick look seems fine to me. Please show your own code that is generating this warning. Use code blocks to post the code, as in
Code:/* This is some C code - note it keeps the indents. */ int main(void) { int* intptr = (int*)malloc(sizeof(int)); *intptr = 1; return *intptr; }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-15-2010 #4Just Joined!
- Join Date
- May 2010
- Posts
- 9
Note that the author is saying that the return is "immediatly unwrapped into a variable". I'm supposing that it is happening in line "p1 = *coord_fn();" but the compiler still generate the warning. To assert my assumptions, I've declared the local variable as static and the warning got out. What a ****? Am I wrong or not?

I'm sure that this material is more reliable than my assumptions, but this question is fooling me...
thnx...Code:#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { float x,y,z; } coord_t; void main(int argc, char **argv){ coord_t p1, *coord_fn(); p1 = *coord_fn(); exit(0); } coord_t *coord_fn(){ coord_t a; a.x = 10; a.y = 11; a.z=13; return &a; }
- 07-15-2010 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Try this instead.
The error/warning was correct, in that you were returning the address of an automatic variable, which goes away when the function call is complete, resulting in an invalid address. By making it static, it will stay around as long as the application runs.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { float x,y,z; } coord_t; coord_t *coord_fn(void) { static coord_t a; a.x = 10; a.y = 11; a.z=13; return &a; } int main(int argc, char **argv) { coord_t p1 = coord_fn(); return 0; }
Also, note that main() is supposed to return an integer these days. Some current compilers, especially if they are in "strict" mode, will barf at the void main() construction. And finally, don't use an empty argument list for a function call. Specify a void argument list, as shown above for coord_fn(void).Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-15-2010 #6Just Joined!
- Join Date
- May 2010
- Posts
- 9
So my assumptions are correct... Thanks for the help. How do you know information like this these ones? the return of main and the void in argument list?
As a java programmer I used to read really reliable books so that I've become a great java programmer. There is some book or any further as good as those that you recommend to me?
thnx
- 07-15-2010 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
These are part of the C89 standard. Most compilers will allow older K&R type of constructs, but the default is not always what you expect. For example, if you declare/define a function with no return type, it will default to an integer on any recent compiler, so failure to return a value should emit a compiler warning or error. An empty argument list is similar to telling the compiler that you will use K&R old style argument declarations outside of the parenthesis, not that there a no arguments. So, ANSI-style function declarations that take no arguments will have an argument list inside the parenthesis of void.
Recommended reference books:
Kernighan and Ritchie, The C Programming Language, 2nd edition or later
ANSI C, A Lexical Guide, Mark Williams Company
There are others as well, but these are well-used on my shelf.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
