Subject: Re: toolchain/22118: make won't compile with -Wcast-qual -Wstrict-prototypes and more
To: NetBSD Toolchain Technical Discussion List <tech-toolchain@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-toolchain
Date: 07/16/2003 02:47:08
>> Using `char foo[] = "string"' does de-const the string.

Not really; it was never const to begin with, because the double-quoted
text is not a string constant in the usual sense; it is an initializer,
an abbreviation for { 's', 't', 'r', 'i', 'n', 'g', '\0' } (modulo the
removal of the trailing '\0' if the string's length exactly matches the
declared size of the array).

> Ah, OK, thanks for clarifying that trick really does do the right
> thing, at least with GCC, I assume.

Not just with gcc; it works with any C compiler.

> However so far as I can tell (from informal sources -- I've not got a
> copy of the final C standard) such treatment of string constants in
> array initializers like that in the example above is not strictly
> compliant with Standard C.

I believe your informal sources are wrong.  Second Edition K&R says
(A8.7)

	As a special case, a character array may be initialized by a
	string literal; successive characters of the string initialize
	successive members of the array.  [...]  If the array has
	unknown size, the number of characters in the string, including
	the terminating null character, determines its size; if the
	size is fixed, the number of characters in the string, not
	counting the terminating null character, must not exceed the
	size of the array.

> From what I read in Harbison and Steele all string constants have the
> type "const char []" and _may_ be allocated in read-only storage.

Second Edition K&R says (A2.6):

	A string literal, also called a string constant, is a sequence
	of characters surrounded by double quotes, as in "...".  A
	string has type "array of characters" and storage class static
	(see ...) and is initialized with the given characters.
	Whether identical string literals are distinct is
	implementation-defined, and the behavior of a program that
	attempts to alter a string literal is undefined.

Note in particular that a string's type does not involve const; that is
a gccism, occuring only in the presence of -Wwrite-strings.  Unless
there is some relevant surrounding language you didn't quote, I think
H&S is wrong to include the const.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B