Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!necntc!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Local string storage -- *guaranteed* static? Message-ID: <512@haddock.UUCP> Date: Wed, 3-Jun-87 22:33:54 EDT Article-I.D.: haddock.512 Posted: Wed Jun 3 22:33:54 1987 Date-Received: Sun, 7-Jun-87 05:38:17 EDT References: <7690@brl-adm.ARPA> Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer) Organization: Interactive Systems, Boston Lines: 25 In article <7690@brl-adm.ARPA> gea@Juliet.Caltech.EDU (Gary Ansok) writes: >Someone asked whether strings in functions like > func() { char *str = "some string"; ... return(str); } >are static, and several people affirmed that they were, but without any >evidence. ... But is there any guarantee, either in K&R or in the Draft >Standard, that this data will be static? Yes. K&R, Appendix A, 2.5 (p. 181): "A string has ... storage class static"; dpANS May86, 3.1.4 (String Literals): "Semantics A string literal has static storage duration ...". >By the way, the question can be avioded if the function is re-written as: > func() { static char str[] = "some string"; ... return(str); } Actually, if you want identical semantics, you'd want func() { static char b[]="some string"; char *str=b; ... return(str); } although you do get the same results if str is otherwise unused -- in which case you might as well simplify to func() { ... return ("some string"); } >Moral: Don't make assumptions when you can tell the compiler what you mean. Moral: RTFM, then assume that what it says is true. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint