Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #58864

Re: string literals vs char*

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: string literals vs char*
Date 2015-03-24 12:17 -0700
Organization None to speak of
Message-ID <87iodqryqg.fsf@kst-u.example.com> (permalink)
References (1 earlier) <ptss9a9fsoeqb304o4vmhht9qffb2414dg@4ax.com> <lnbnmozx3e.fsf@nuthaus.mib.org> <m7od1e$rnv$1@speranza.aioe.org> <87k31cdmv4.fsf@bsb.me.uk> <kfnr3seie4o.fsf@x-alumni2.alumni.caltech.edu>

Show all headers | View raw


Tim Rentsch <txr@alumni.caltech.edu> writes:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:
>>> Keith Thompson <kst-u@mib.org> 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  <http://www.ghoti.net/~kst>
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"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

string literals vs char* arnuld <sunrise@invalid.address> - 2014-12-27 05:37 +0000
  Re: string literals vs char* Kaz Kylheku <kaz@kylheku.com> - 2014-12-27 06:36 +0000
  Re: string literals vs char* Barry Schwarz <schwarzb@dqel.com> - 2014-12-27 00:58 -0800
    Re: string literals vs char* Ian Collins <ian-news@hotmail.com> - 2014-12-27 22:03 +1300
    Re: string literals vs char* Keith Thompson <kst-u@mib.org> - 2014-12-27 11:55 -0800
      Re: string literals vs char* glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-28 07:55 +0000
        Re: string literals vs char* Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-28 11:39 +0000
          Re: string literals vs char* Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 08:55 -0700
            Re: string literals vs char* Keith Thompson <kst-u@mib.org> - 2015-03-24 12:17 -0700
  Re: string literals vs char* James Kuyper <jameskuyper@verizon.net> - 2014-12-27 11:36 -0500
    Re: string literals vs char* Vincenzo Mercuri <nomail@yahoo.it> - 2014-12-27 18:20 +0100
      Re: string literals vs char* Vincenzo Mercuri <nomail@yahoo.it> - 2014-12-27 18:55 +0100
    Re: string literals vs char* Kaz Kylheku <kaz@kylheku.com> - 2014-12-27 17:25 +0000
    Re: string literals vs char* Keith Thompson <kst-u@mib.org> - 2014-12-27 12:08 -0800
    Re: string literals vs char* James Kuyper <jameskuyper@verizon.net> - 2014-12-28 07:01 -0500
  Re: string literals vs char* John Bode <jfbode1029@gmail.com> - 2014-12-30 10:52 -0800

csiph-web