开发者

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, o开发者_开发百科r 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 ?


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


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 };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜