What will be the value of un-initialized element in the array?

前端 未结 1 601
粉色の甜心
粉色の甜心 2021-01-18 13:39

I have the following code

int t[] = {
        [1] = 45,
        [2] = 33
    };

What will be the value of t[0] in this case? I

相关标签:
1条回答
  • 2021-01-18 14:11

    The other value(s) will be initialized; from C11 standard, §6.7.9 Initialization, ¶19 and ¶21:

    The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

    151) Any initializer for the subobject which is overridden and so not used to initialize that subobject might not be evaluated at all.


    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

    And uninitialized int with static storage duration are implicity initialized to zero; same section, ¶10:

    If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

    • if it has pointer type, it is initialized to a null pointer;
    • if it has pointer type, it is initialized to a null pointer;
    • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
    • if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

    The behavior is the same in C89 and C99.

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