Initializing arrays of char

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Sat Oct 6 14:56:13 AEST 1990


In article <3568 at dftsrv.gsfc.nasa.gov> jim at jagmac2.gsfc.nasa.gov (Jim Jagielski) writes:
> You forget that doing "char baz[] = "12345";" is the same as:
> 	char baz[] = { '1','2','3','4','5','\0' };

That's what I said. Null-terminated character arrays are called strings,
and my point was that the original poster was *not* asking about them.

Again, the right way 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.

---Dan



More information about the Comp.lang.c mailing list