Array initialization question
Robert Carey
carey at eniac.seas.upenn.edu
Wed May 15 01:28:41 AEST 1991
The SunOS C compiler seems to be doing me an unwanted favor. If in
defining a multidimensional array of char I initialize a row using a
string which is one character longer than the row (admittedly a bad
thing to be doing - it was an accident), it inserts all the values up
to and not including the NUL byte, and does not issue a warning. (More
likely the second initializer actually clobbers the NUL byte from the
first one.) If I try to use a string that is even one character longer
than that it prints a message and truncates the initialization string
to one less than the length of the row and inserts a NUL in the last
byte of the row. I would have expected the compiler to give me a
warning in both cases. Is it supposed to work this way?
Example initializing a 5 character row from a 6 character string:
$ cat foo.c
main()
{
static char foo[2][5]={"12345", "67890"};
printf("[%s][%s]\n", foo[0], foo[1]);
}
$ cc -o foo foo.c
$ foo
[1234567890][67890]
$
Example initializing a 5 character row from a 7 character string:
$ cat foo.c
main()
{
static char foo[2][5]={"123456", "678901"};
printf("[%s][%s]\n", foo[0], foo[1]);
}
$ cc -o foo foo.c
"foo.c", line 3: warning: string initializer too long
"foo.c", line 3: warning: string initializer too long
$ foo
[1234][6789]
$
More information about the Comp.lang.c
mailing list