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

前端 未结 2 1529
眼角桃花
眼角桃花 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 };
    

提交回复
热议问题