Does a string literal count as a partial initializer and zero-initialize?

前端 未结 3 1794
逝去的感伤
逝去的感伤 2020-12-06 05:13

In C, you can partially initialize a struct or array, with the result that the members/elements that aren\'t mentioned in the initializer are zero-initialized. (C99

3条回答
  •  伪装坚强ぢ
    2020-12-06 06:12

    From the C99 standard (as already stated by ouah):

    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:

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

    • 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;
    • if it is a union, the first named member is initialized (recursively) according to these rules.

    And char is an arithmetic type, so the remaining elements of the array will be initialised to zero.

提交回复
热议问题