tags:

views:

80

answers:

1

When I declare a two-dimensional array like this:

char myArray[20][30] = {"ABC", "Is Easy As One Two Three"};

Can I assume that all other chars in this array are now set to \000?

+1  A: 

Objects are never partially initialised in C - if you have used an initialiser, then the entire object is guaranteed to be initialised (in this case, the "object" is the whole array of 20 arrays of 30 chars each). Any members not explicitly initialised are recursively initialised to zero (for arithmetic types), or NULL (for pointer types).

So the answer, in this case, is yes - all the chars not explicitly given values by the initialiser are guaranteed to be 0.

This is described in the C99 standard in section 6.7.8, Initialisation. The relevant paragraphs are:

21 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.

and

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

— if it is a union, the first named member is initialized (recursively) according to these rules.

caf