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

前端 未结 2 1509
眼角桃花
眼角桃花 2021-01-18 01:44

Given

char foo[1024] = \"bar\";

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

相关标签:
2条回答
  • 2021-01-18 02:30

    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 讨论(0)
  • 2021-01-18 02:36

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

    0 讨论(0)
提交回复
热议问题