array-type initialization

From cppreference.com
< c‎ | language

Template:c/language/initialization/navbar

This is the initialization performed when an array is constructed.

[edit] Explanation

If an array has automatic storage duration and is not initialized explicitly, its initial values are indeterminate.

If an array has static or thread storage duration and is not initialized explicitly and the array elements have

  • pointer type, each element is initialized to a null pointer;
  • arithmetic type, each element is initialized to (positive or unsigned) zero;
  • aggregate type, each element is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
  • union type, each element is initialized such that the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits.

When no designator is present, initialization of array elements occurs in increasing subscript order. Designators can change the order of the initialization of array elements.

If the array contains elements or members that are arrays, structures, or unions, these rules apply recursively to the sub-arrays, sub-structures, or contained unions.

If an array of known size is initialized, array elements that are not initialized explicitly are initialized implicitly the same as elements that have static storage duration.

If an array of unknown size is initialized, the largest indexed element with an explicit initializer determines the size of the array.

The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified.

[edit] Example

#include <stdio.h>
#include <wchar.h>
 
struct S {
    char c;
    int  i;
};
union U {
    short int si;
    int        i;
    long int  li;
};
 
int main(void)
{
    int i1[5];                        /* initial values are indeterminate          */
    static int i2[5];                 /* static storage duration: 0,0,0,0,0        */
    int i3[5] = {1,2,3,4,5};          /* increasing subscript order                */
    int i4[5] = {[4]=5,[0]=1,2,3,4};  /* designators change initialization order   */ 
    int i5[5] = {1,2,3};              /* too few initializers: 1,2,3,0,0           */
    int i6[]  = {1,2,3};              /* array of unknown size                     */
    int i7[5] = {0};                  /* all array elements zero                   */
    int i8[4][3] = {                  /* sub-array: fully bracketed initialization */
                    {1,2,3},
                    {4,5,6},
                    {7,8,9},
                    {10,11,12}
                   };
    struct S i9[2] = { {'a',1},       /* sub-structure */
                       {'b',2}
                     };
    union U i10[2] = { {1},           /* sub-union */
                       {2}
                     };
 
    char    c1[5] = {'a','b','c','d','e'};   /* holds character array          */
    char    c2[5] = "abcd";                  /* holds character string literal */
    char    c3[5] = {"abcd"};
    char    c4[5] = "abcde";                 /* holds character array          */
    wchar_t c5[5] = {L'a',L'b',L'c',L'd',L'e'};
    wchar_t c6[5] = L"abdc";
 
    return 0;
}

Possible output:

(none)