## question on memory allocation for char strings ##

Tim Writer writer at me.utoronto.ca
Fri Jun 29 10:13:29 AEST 1990


In article <601 at babcock.cerc.wvu.wvnet.edu> siping at cathedral.cerc.wvu.wvnet.edu (Siping Liu) writes:

>main()
>{
>  f1(f2());
>}

>f1(parm1)
>char parm1[];
>{
>  printf("%s", parm1);
>}

>char *f2()
>{
>  char abc[100];

>  strcpy(abc, "12345");

>  return(abc);
>}

>The question is: can I get the correct print out in function "f2"
>as I have set in function "f1" ?? After the program returned from "f2",
>memory space for "abc" was freed and may be used when calling "f1". So
>even I get a correct print out, I still question if it's correct.

If you declare abc to be static in f2(), it will be permanent.  That is, it
will not be freed on return from f2().  This will guarantee that printf()
in f1() prints the correct string.

Tim




More information about the Comp.lang.c mailing list