Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!unix.cis.pitt.edu!dsinc!netnews.upenn.edu!eniac.seas.upenn.edu!carey From: carey@eniac.seas.upenn.edu (Robert Carey) Newsgroups: comp.lang.c Subject: Array initialization question Message-ID: <43075@netnews.upenn.edu> Date: 14 May 91 15:28:41 GMT Sender: news@netnews.upenn.edu Reply-To: carey@eniac.seas.upenn.edu (Robert Carey) Distribution: usa Organization: University of Pennsylvania Lines: 42 Nntp-Posting-Host: eniac.seas.upenn.edu 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] $