When initializing a char array, is the remaining space null padding or non-initialization?

Considering

char foo[1024] = "bar"; 

Does this initialize foo to contain 'b', 'a', 'r', 0. Are the remaining 1020 characters zero initialized or uninitialized?

I would think the above is the same as `char foo [1024] = {'b', 'a', 'r', '\ 0'}; and how when initializing aggregates any member not mentioned is initialized to zero?

+7
source share
2 answers

If the array / aggregate is somehow initialized [edit: using a static initializer], the rest of the unspecified entries are reset, yes.

+9
source

Yes, uninitialized array elements will be zeros. Example:

If the initializer feeds too few elements, 0 is accepted for the remaining elements of the array:

 int v5[8] = { 1 , 2 , 3 , 4 }; 

equivalently

 int v5[] = { 1 , 2 , 3 , 4 , 0 , 0 , 0 , 0 }; 
+3
source

All Articles