Subject: Re: toolchain/22118: make won't compile with -Wcast-qual
To: NetBSD Toolchain Technical Discussion List <tech-toolchain@NetBSD.ORG>
From: Todd Vierling <tv@pobox.com>
List: tech-toolchain
Date: 07/17/2003 16:33:23
On Thu, 17 Jul 2003, Greg A. Woods wrote:

: However the other related question remains:  What does the C standard
: say is default implied type of a string literal when used as an
: initializer for a pointer type (as opposed to the array type above)?

I can't say if this is in the published standard, as I'm not shelling out
the 200 bucks for a personal copy, but in a 1998 draft of the Next
Generation of ISO C (linked off of www.lysator.liu.se/c), the following
appears immediately after bjh21's quote:

       The contents of the arrays are  modifiable.   On  the  other
       hand, the declaration

               char *p = "abc";

       defines  p  with type ``pointer to char'' and initializes it
       to point to an object  with  type  ``array  of  char''  with
       length  4  whose  elements  are initialized with a character
       string literal.  If an attempt is made to use  p  to  modify
       the contents of the array, the behavior is undefined.
                                                  ^^^^^^^^^

Don't-cha just love the number of times the word "undefined" appears in
these standards?

: It would seem to me that GCC is far closer to what should be considered
: correct.  Magically changing the way a string literal is interpreted
: when it is used in the place of a "char *" initializer as opposed to
: when it is used in place of an expression term with the type "char *"
: would not be very useful.

Well, the (1998 draft) standard doesn't make these two situations different
per se.  If initializers are treated the same way as expressions, the
"undefined" behavior above would be that the initializer's storage is also
nonwritable.

: I.e. in practice portable code must assume that string constants, even
: when used as "char *" initializers (but also when used in expressions),
: have an implied "const" qualifier

Yup, in order to avoid the "undefined" case.

-- 
-- Todd Vierling <tv@pobox.com>