Path: utzoo!utgpu!news-server.csri.toronto.edu!me!writer Newsgroups: comp.lang.c From: writer@me.utoronto.ca (Tim Writer) Subject: Re: ## question on memory allocation for char strings ## Message-ID: <90Jun28.201314edt.19443@me.utoronto.ca> Organization: University of Toronto, Department of Mechanical Engineering References: <601@babcock.cerc.wvu.wvnet.edu> Date: 29 Jun 90 00:13:29 GMT In article <601@babcock.cerc.wvu.wvnet.edu> siping@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