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


Groups > comp.lang.c > #55539

Re: string literals vs char*

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.lang.c
Subject Re: string literals vs char*
Date 2014-12-28 07:55 +0000
Organization Aioe.org NNTP Server
Message-ID <m7od1e$rnv$1@speranza.aioe.org> (permalink)
References <549e45b4$0$280$14726298@news.sunsite.dk> <ptss9a9fsoeqb304o4vmhht9qffb2414dg@4ax.com> <lnbnmozx3e.fsf@nuthaus.mib.org>

Show all headers | View raw


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. 

A fair number of compilers might still allow it, if they don't test
for it, and don't keep constants in write protected storage.
 
> would actually print "Hello".  (It might also affect other uses of the
> same string literal.)  And in fact a conforming C11 compiler could do
> the same thing; the behavior is undefined, so it can do anything it
> likes.

Yes, a compiler that specifically allowed it should keep all string
constants stored separately.
 
But as the OP wrote:

>>>[arnuld@arch64 c $] cat test.c
>>>#include<stdio.h>
>>>
>>>int main(void)
>>>{
>>>    char *p1 = "India", *p2;
>>>    p2=p1;
>>>    p1 = "BIX";
>>>    printf("%s %s\n", p1, p2);
>>>    return 0;
>>>}

It seems to me that this doesn't actually modify any constants.

-- glen

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