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


Groups > comp.lang.c > #55491

Re: string literals vs char*

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: string literals vs char*
Date 2014-12-27 11:36 -0500
Organization A noiseless patient Spider
Message-ID <m7mn61$pti$1@dont-email.me> (permalink)
References <549e45b4$0$280$14726298@news.sunsite.dk>

Show all headers | View raw


On 12/27/2014 12:37 AM, arnuld wrote:
> C90 requires that string literls are constant, then why I am not getting 
> warning here.

It does not require that they be constant. It says that the behavior is
undefined if you attempt to modify them. "undefined behavior" is a piece
of jargon defined by the C standard. It is NOT a requirement, rather, it
identifies a complete absence of requirements imposed by the C standard.
There have been real implementations where the program would abort() if
such code were executed, and other implementations where executing that
code would cause the memory containing the string literal to be
modified. In principle, there could be implementations which silently
converted such operations into no-ops, though I'm not personally aware
of any. As there is a complete absence of requirements imposed by the C
standard on such code, all three behaviors are permitted for a
conforming implementation.

String literals have the type char[n], where n is 1 more than the number
characters in the string literal, and in most contexts are converted
into pointers to the first element of that array. That pointer has the
type 'char*'. As a result, your code doesn't violate any constraints.

> ... Is this a compiler problem ?  (if it is a compiler problem 
> then why g++ catches the error at compile time when gccdoes not):

The type of a string literal should have been "const char[n]", But C was
designed long before the 'const' keyword was added, and far too much
legacy code would have had to be re-written if they had changed that
rule when 'const' was added. 'const' was invented for C++, and ported
back to C, and in C++ string literals have been const-qualified almost
from the very beginning.

> [arnuld@arch64 c $] cat test.c
> #include<stdio.h>
> 
> int main(void)
> {
>     char *p1 = "India", *p2;

A sufficiently helpful C compiler could warn about that initialization,
but there's nothing in the C standard that mandates it.

>     p2=p1;
>     p1 = "BIX";

The same is true about that assignment.
-- 
James Kuyper

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