Initializing arrays of char
Conor P. Cahill
cpcahil at virtech.uucp
Sat Oct 6 05:15:43 AEST 1990
In article <15674 at csli.Stanford.EDU> poser at csli.stanford.edu (Bill Poser) writes:
>This means that the assignment of "12345" to an array of five characters,
>is legal. If K&R2 here reflects the standard, then both initializations
>are legitimate.
While it is "legal" it still should get a warning since it is doing something
that you may not expect.
>This seems to me to be a bad idea. Everywhere else, one has to take
>into account the terminating null. For example, x[5] = 'a' is
>an error. Not counting the terminating null here is inconsistent.
This has nothing to do with a terminating null. x[5] is illegal because
you are accessing an element beyound the end of the array (assuming it
was declared as char x[5]).
>Can anyone explain this decision?
Probably because that was the existing standard (the way C has worked all
along).
Another way to look at this is that "char x[dim];" declares an array
of characters, not a character string. So the null need not be there and
without this rule you couldn't initialize the last element of the
array to be a non-null.
--
Conor P. Cahill (703)430-9247 Virtual Technologies, Inc.,
uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160
Sterling, VA 22170
More information about the Comp.lang.c
mailing list