returning automatic variables

Doug Gwyn gwyn at smoke.brl.mil
Sat May 25 07:11:35 AEST 1991


In article <1991May24.153133.13590 at ux1.cso.uiuc.edu> nnj20229 at uxa.cso.uiuc.edu (Nesha Nicole Jones) writes:
>char *word(void)
>{
>        char name[100];
...
>        return name;
>}

You're not returning the value of an auto variable, but rather a pointer
to it.  However, the auto variable "goes out of scope" (becomes invalid)
upon return from the function; therefore what is pointed at by the
returned pointer is no longer valid.

There are several possible solutions, the simplest being to declare the
name[] array as having static storage duration.



More information about the Comp.lang.c mailing list