Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: string literals vs char* Date: Tue, 24 Mar 2015 12:17:59 -0700 Organization: None to speak of Lines: 70 Message-ID: <87iodqryqg.fsf@kst-u.example.com> References: <549e45b4$0$280$14726298@news.sunsite.dk> <87k31cdmv4.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="c77ff4d35e33d1c939d3461ebde8dd0d"; logging-data="2400"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Kbhks0fjgoLg6OIDj8eaq" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:2Prp/1IMb4oe7P0iYvenF8Ng2b4= sha1:pDZr8D2LekN+wXMZ2T2Ll2EcnNM= Xref: csiph.com comp.lang.c:58864 Tim Rentsch writes: > Ben Bacarisse writes: >> glen herrmannsfeldt writes: >>> Keith Thompson wrote: >>> (snip) >>> >>>> All this applies to C90, C99, and C11. >>>> >>>> Yes, there's a difference between constant and const, but you haven't >>>> stated it correctly. >>>> >>>> A *constant expression* is one that can be (and in some cases must be) >>>> evaluated at compile time. (And a "constant", used as a noun, is a >>>> token in source code, referred to in some languages as a "literal".) >>> >>> (snip) >>> >>>> Some pre-ANSI C compilers, IIRC, actually allowed you to modify string >>>> literals, so that this: >>>> >>>> char *s = "hello"; >>>> s[0] = 'H'; >>>> puts(s); >>> >>> I don't have my K&R1 nearby, but I thought I remember that it >>> specifically allowed for this. >> >> I can't find any explicit text either way, but that suggests, to me, >> that the arrays denoted by string literals are just like any other (K&R) >> C arrays and may be modified. >> >> Further evidence comes form that fact the K&R1 does say that all >> strings, even when written identically, are distinct. That's not >> conclusive, but sharing between similar strings is one reason to make >> writing to them undefined. > > For those who are interested: > > At the time that the C89 committee was working, writable > string literals weren't "legacy code" (Margolin) and what > standard there existed (K&R 1) was quite explicit (A.2.5) > that strings were just a way of initializing a static array. > And as Barry pointed out there were some (mktemp) routines > that used this fact. > > This comment comes from a comp.std.c posting in 1998, written > by Dennis Ritchie. I happen to have my copy of K&R1 handy. Appendix A is the C Reference Manual. A.2.5 says: A string is a sequence of characters surrounded by double quotes, as in "...". A string has type "array of characters" and storage class static (see section 4 below) and is initialized with the given characters. All strings, even when written identically, are distinct. [...] I do not guarantee that I haven't introduced any typos. There was no notion of "const", so the array was implicitly writable. A.2.5 uses the term "string" to refer both to what we would now call a "string literal" (K&R1 uses the phrase "string constant" elsewhere), and to corresponding the static array that exists during program execution. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"