Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!amdcad!amd!markg From: markg@amd.UUCP Newsgroups: comp.lang.c Subject: Re: Local string storage -- *guaranteed* static? Message-ID: <4075@amd.UUCP> Date: Thu, 4-Jun-87 13:38:31 EDT Article-I.D.: amd.4075 Posted: Thu Jun 4 13:38:31 1987 Date-Received: Sat, 6-Jun-87 08:24:31 EDT References: <7690@brl-adm.ARPA> Reply-To: markg@amd.UUCP (Mark Gorlinsky) Organization: Advanced Micro Devices Lines: 50 In article <7690@brl-adm.ARPA> gea@Juliet.Caltech.EDU (Gary Ansok) writes: >Someone asked whether strings in functions like > >[char *]func() { > char *str = "some string"; > ... > return(str); >} > >are static, and several people affirmed that they were, but without >any evidence. >[deleted] > But is there any guarantee, either in K&R >or in the Draft Standard, that this data will be static? > K&R pg 80 para 4.6 Static Variables ... "Static variables may be either internal or external. Internal static variables are local to a particular function just as automatic variables are, but unlike automatics, they remain in existence rather than coming and going each time the function is activated. This means static variables are private, permanent storage in a function (automatics are just private). Character strings that appear within a function, such as the arguments of printf, are internal static." ... If str wasn't static then the following program would print "hello" instead of "hXllo". char *func(); main() { char *str2; str2 = func(); str2[1] = 'X'; str2 = func(); printf("String str2 = %s\n", str2); } char *func() { char *str = "hello"; return (str); } -- Mark Gorlinsky - AMD Processor Products Division/APPS SQA UUCP: {decwrl,ihnp4,allegra}!amd!markg AT&T: (408) 982-7811 DISCLAIMER: My opinions are mine, not my employers.