Initializing arrays of char

Mark Brader msb at sq.sq.com
Mon Oct 8 05:47:40 AEST 1990


> Again, the right way

(as a point of style, he means)

> to initialize a five-element character array is to
> list the five characters explicitly:
>      char baz[5] = { '1', '2', '3', '4', '5' } ;
> If you use "12345", you'll confuse the reader (not to mention any old
> compilers) into thinking that you really want a (0-terminated) string.

This may be true if you're dealing with 5-character arrays, but it
fails as soon as there are too many initializers to count by eye.
Suppose it was:

	char parity[64] = 
	    "EOOEOEEOOEEOEOOEOEEOEOOEEOOEOEEOOEEOEOOEEOOEOEEOEOOEOEEOOEEOEOOE";

It's obvious from its content that this is not a string to be printed,
so the absence of a trailing null* should not cause confusion.  You could
add a one-line comment if you you really must.  But I would (mildly) prefer
to see the one line above than four lines of 'E', 'O', 'O', 'E', 'O', ...

*or NUL, or '\0', but, please, never NULL.
-- 
Mark Brader			"Metal urgy.  The urge to use metals.
SoftQuad Inc., Toronto		 That was humans, all right."
utzoo!sq!msb, msb at sq.com			-- Terry Pratchett: Truckers

This article is in the public domain.



More information about the Comp.lang.c mailing list