[No!] Web doc bug: adding \0 to char array??

[Edit: This post is wrong. C does initialize static storage to 0. But it also (which I didn't know) fills out partially-specified arrays with 0. So there's no problem with the documentation.]

The documentation at http://www.arduino.cc/en/Reference/String for "char *" type strings claims that this is a valid string initialization:

char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};

It further claims that the compiler will add the (almost always desired) \0 character to the end.

I think this is subtly wrong. If Str2 is a static (e.g. global) variable, then the 8th position would be cleared to 0 - just like any static variable - and it wouldn't matter that you didn't initialize it. So in this case, it looks like the compiler added the \0 to the initialization array.

But if Str2 were declared as a function local variable, I think the 8th position would be stack garbage.

At least, that's the case in C. Maybe C++ is different, but it still seems like a shady thing to teach programmers to depend on.

Consider this declaration:

char Str7[7] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};

Should the compiler add a \0 to this? Obviously not.

char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};

According to the C standard (C++ also):

If you declare an array with a known size and you give it an initializer list with fewer elements than the size of the array, the remaining elements will be set to zero. (So Str2[7] is set to a zero byte, '\0'.)

char Str7[7] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};

Should the compiler add a \0 to this? Obviously not.

Since the number of elements in the initializer list is equal to the number of elements that you declared, the compiler will fill the array exactly as you told it to.




In the C language standard document ISO/IEC 9899

Section 6.7.8, paragraph 19.

[color=#0000ff]If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.[/color]

Note that this is one of the very, very few things in Standard C that is different from the language description in the Second Edition of K&R.

The language is more complicated for C++ (since it is concerned with classes and not just simple arrays), but the outcome is the same.

The bottom line for me is that, as a matter of style, I would (probably) not initialize a C-style "string" by declaring an array like this and giving it an initializer list with fewer characters than the size of the array. Nevertheless, it is not "wrong" to do so, according to the language standards documents. Whether it is a "good idea" to encourage new programmers to get into the habit of engaging in such practices, well...

Thanks, Dave. You're right, and I've edited my post.

I thought the doc was saying that the \0 would be added for some string-specific reason. It was not saying that, of course.