Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!bbn!cc5.bbn.com!keesan From: keesan@cc5.bbn.com.UUCP Newsgroups: comp.lang.c Subject: Re: static variables Message-ID: <1546@cc5.bbn.com.BBN.COM> Date: Thu, 4-Jun-87 16:15:31 EDT Article-I.D.: cc5.1546 Posted: Thu Jun 4 16:15:31 1987 Date-Received: Sat, 6-Jun-87 07:05:48 EDT References: <7653@brl-adm.ARPA> <4064@amd.UUCP> <183@inco.UUCP> Reply-To: keesan@cc5.bbn.com.UUCP (Morris M. Keesan) Organization: Bolt Beranek and Newman, Cambridge, MA Lines: 36 Summary: variable is automatic, string is static In article <183@inco.UUCP> mack@inco.UUCP (Dave Mack) writes: >In article <4064@amd.UUCP>, markg@amd.UUCP (Mark Gorlinsky) writes: >> In article <7653@brl-adm.ARPA> jfjr@mitre-bedford.arpa writes: >> >Are character strings local to a function static?? >> Yes!!!!!!!!! >I disagree. A static variable retains its value between function >calls, while an initialized local variable is reinitialized on >each call to the function. Try this code to see what I mean: >#include >main() >{ > out(); > out(); >} >out() >{ > char *s = "Hello"; > static char *s2 = "World!"; > printf("%s %s\n",s,s2); > s = "Goodbye"; > s2 = "Mom"; >} Basic confusion here. s and s2 are automatic variables, "Hello", "World!", "Goodbye", and "Mom" are static strings. (Ref: K&R p. 193, sec. 8.1; p. 181, sec. 2.5). Instead s = "Goodbye" and s2 = "Mom" in the above, try s[0] = 'J'; s2[0] = 'M'; s2[2] = 'l'; In the original example, { char *some_string = "some string"; }, some_string is automatic and "some string" is static. -- Morris M. Keesan keesan@bbn.com {harvard,decvax,ihnp4,etc.}!bbn!keesan