Array initialization question

Chris Torek torek at elf.ee.lbl.gov
Wed May 15 06:43:52 AEST 1991


In article <43075 at netnews.upenn.edu> carey at eniac.seas.upenn.edu
(Robert Carey) writes:
>      static char foo[2][5]={"12345", "67890"};

The ANSI C standard (X3.159-1989) explicitly says that if you use a
string literal as an initializer for a character array and you have
specified the size of the array and the string literal exactly fills
the array when the trailing '\0' character is dropped, that is what you
get.  Thus, this creates two adjacent blocks of 5 `char's and sets them
to:

	'1' '2' '3' '4' '5' '6' '7' '8' '9' '0'

(Some argued that this was a misfeature, but alternative proposals such
as `\c' for suppressing trailing NULs in string literals were
eventually rejected.  Some find the whole thing incomprehensible:  if
you wanted this result you could have used

	static char foo[2][5] = {
		{'1', '2', '3', '4', '5'},
		{'6', '7', '8', '9', '0'},
	};

I tend toward the latter camp; if a mechanism for omitting the trailing
NUL in string literals is to be required, I tend toward the former camp.)
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list