Habalusa
Habalusa

Reputation: 1800

When initializing a char array, is the remaining space zero filled or uninitialized?

Given

char foo[1024] = "bar";

This will initialize foo to contain 'b','a','r',0 . Is the remaining 1020 characters zero initialized, or uninitialized ?

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

Upvotes: 7

Views: 754

Answers (2)

Chaithra
Chaithra

Reputation: 1140

Yes, the uninitialized array elements will be zeroes. Example:

If the initializer supplies too few elements, 0 is assumed for the remaining array elements:

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

is equivalent to

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

Upvotes: 3

user562374
user562374

Reputation: 3897

If an array/aggregate is initialized somehow[edit: by use of a static initializer], the remaining unspecified entries are zeroed, yes.

Upvotes: 9

Related Questions