Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!samsung!usc!apple!rutgers!bellcore!dduck!leo From: leo@dduck.ctt.bellcore.com (Leo Zvenyatsky) Newsgroups: comp.sys.ibm.pc Subject: Re: TC function returns rubbish. why? Summary: solution - static declaration Keywords: TC20 char *function returns bad data Message-ID: <18515@bellcore.bellcore.com> Date: 7 Dec 89 14:40:35 GMT References: <272@cica.cica.indiana.edu> Sender: news@bellcore.bellcore.com Reply-To: leo@dduck.UUCP (Leo Zvenyatsky) Distribution: na Organization: Bellcore, Piscataway, NJ Lines: 34 In article <272@cica.cica.indiana.edu> burleigh@cica.cica.indiana.edu (Frank Burleigh) writes: > ... >char *make_fn( char * file_name ) >{ > int i; > char *w, *p = file_name; > char name[13]; > > for( w = name; *p; p++ ) > if( *p != ' ' ) > *w++ = *p; /*don't copy ws*/ > *w = '\0'; > return name; >} > ... stuff deleted ... Sorry for posting - mail bounced. The problem is that name[] is NOT static (it is dynamic) and IS local to make_fn. When make_fn returns, all of its dynamic storage is returned to the free pool and may or may not be overwritten by anything else. The point is that you CANNOT trust it! The simplest solution is to declare name[] static, i.e. char static name[13]. Another solution is to pass to it a char pointer pointing to a storage within the CALLING routine, and have make_fn populate that array. Since calling routine has not yet completed, all of its storage is still intact. Hope that helps. _ ___ | If any views or opinions are found _// / | in this article, please return to: / _ __ / | Leo Zvenyatsky /___