From: sasghm@theseus.unx.sas.com (Gary Merrill)
Subject: Re: Strings and integer sizes
Originator: sasghm@theseus.unx.sas.com
Date: Thu, 15 Oct 1992 13:51:53 GMT
Organization: SAS Institute Inc.


In article <442@ulogic.UUCP>, hartman@ulogic.UUCP (Richard M. Hartman) writes:
	[ quotes from K&R omitted ]
|> 
|> This seems to establish that the size of the char array "the" is
|> clearly 4, and therefore should not be allowed as an initializer
|> for a char array of size 3.

                           ...

|> Am I missing something?

Yes.  What you are missing is that "the" is *not* a char array,
as you say above.  It is a *string literal*.  The size of this
*string literal* is 4, but nothing follows from this (in the
context of K&R) concerning the size of any array that might
be initialized by *using* the string literal.

At least, this is what I was *going* to say until I looked more
closely at the text.  Now I think you may in fact have a case
for claiming incompatibility with K&R, but the argument needs
a detail or two you omitted.  Try this ...

In at least two places, the claim is made in K&R that a declaration
of the form

	char array[] = "string";

is *shorthand* for

	char array[] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};

(One is on p 84 and the other is on p. 199 where the word
"abbreviation" is used.)

Thus one would expect, in the absence of any countervailing
discussion that

	char array[7] = "string";

is *shorthand* for

	char array[7] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};

(Though, as you point out, no such examples actually occur in
the text.)  Thus likewise, in the context of K&R,

	char array[6] = "string";

is *shorthand* for

	char array[6] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};

But the latter is an *error* :

	It is an error to have too many initializers.  (p. 84)

and so, from the point of view of K&R, since the latter is simply
shorthand for the former, then the former should be an error as
well.  Yet this is just what ANSI permits.

In short, this argument rests on the repeated claim in K&R that
in array initializations, a string literal may be used as "shorthand"
or as "an abbreviation" of an initializer list.  Yet the consequence
of the ANSI standard is to *reject* this equivalence.  And thus
it is incompatible with K&R in this respect.

So what do we say about all those compilers that, pre-ANSI, allowed
such an initialization as

	char array[6] = "string";

?  Well, they simply weren't K&R conforming, however much their
implementors thought they were.

The weakness in this argument is that K&R does not *explicitly*
forbid this type of intitialization, and it could be argued that
a consistent extension could be made to K&R by saying something
like "In the case that the array size is specified, ..."  However,
I do believe that if this were done, the general claims about
the one form being *shorthand* for the other would need to be
modified.  As it stands, I think the argument for incompatibility
is the stronger one.
-- 
Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
Xref: sparky comp.lang.c:15071 comp.lang.c++:15043