Initialization of arrays

The initializer for an array is a comma-separated list of constant expressions enclosed in braces ({ }). The initializer is preceded by an equal sign (=). You do not need to initialize all elements in an array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. The same applies to elements of arrays with static storage duration. (All file-scope variables and function-scope variables declared with the static keyword have static storage duration.)

There are two ways to specify initializers for arrays:
  • With C89-style initializers, array elements must be initialized in subscript order.
  • C only Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order. Designated initializers are described in detail in Designated initializers for aggregate types (C only). C only
Using C89-style initializers, the following definition shows a completely initialized one-dimensional array:
static int number[3] = { 5, 7, 2 };
The array number contains the following values: number[0] is 5, number[1] is 7; number[2] is 2. When you have an expression in the subscript declarator defining the number of elements (in this case 3), you cannot have more initializers than the number of elements in the array.
The following definition shows a partially initialized one-dimensional array:
static int number1[3] = { 5, 7 };
The values of number1[0] and number1[1] are the same as in the previous definition, but number1[2] is 0.
Begin C only
The following definition shows how you can use designated initializers to skip over elements of the array that you don't want to initialize explicitly:
static int number[3] = { [0] = 5, [2] = 7 };
The array number contains the following values: number[0] is 5; number[1] is implicitly initialized to 0; number[2] is 7.
End C only
Instead of an expression in the subscript declarator defining the number of elements, the following one-dimensional array definition defines one element for each initializer specified:
static int item[ ] = { 1, 2, 3, 4, 5 };
The compiler gives item the five initialized elements, because no size was specified and there are five initializers.

Initialization of character arrays

You can initialize a one-dimensional character array by specifying:
  • A brace-enclosed comma-separated list of constants, each of which can be contained in a character
  • A string constant (braces surrounding the constant are optional)

Initializing a string constant places the null character (\0) at the end of the string if there is room or if the array dimensions are not specified.

The following definitions show character array initializations:
   static char name1[ ] = { 'J', 'a', 'n' };
   static char name2[ ] = { "Jan" };
   static char name3[4] = "Jan";

These definitions create the following elements:

Element Value Element Value Element Value
name1[0] J name2[0] J name3[0] J
name1[1] a name2[1] a name3[1] a
name1[2] n name2[2] n name3[2] n
    name2[3] \0 name3[3] \0
Note that the following definition would result in the null character being lost:
   static char name3[3]="Jan";

C++ only When you initialize an array of characters with a string, the number of characters in the string — including the terminating '\0' — must not exceed the number of elements in the array. C++ only

Initialization of multidimensional arrays

You can initialize a multidimensional array using any of the following techniques:
  • Listing the values of all elements you want to initialize, in the order that the compiler assigns the values. The compiler assigns values by increasing the subscript of the last dimension fastest. This form of a multidimensional array initialization looks like a one-dimensional array initialization. The following definition completely initializes the array month_days:
       static month_days[2][12] =
       {
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
        31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
       };
  • Using braces to group the values of the elements you want initialized. You can put braces around each element, or around any nesting level of elements. The following definition contains two elements in the first dimension (you can consider these elements as rows). The initialization contains braces around each of these two elements:
       static int month_days[2][12] =
       {
        { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
        { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
       };
  • Using nested braces to initialize dimensions and elements in a dimension selectively. In the following example, only the first eight elements of the array grid are explicitly initialized. The remaining four elements that are not explicitly initialized are automatically initialized to zero.
          static short grid[3] [4] = {8, 6, 4, 1, 9, 3, 1, 1};
    The initial values of grid are:
    Element Value Element Value
    grid[0] [0] 8 grid[1] [2] 1
    grid[0] [1] 6 grid[1] [3] 1
    grid[0] [2] 4 grid[2] [0] 0
    grid[0] [3] 1 grid[2] [1] 0
    grid[1] [0] 9 grid[2] [2] 0
    grid[1] [1] 3 grid[2] [3] 0
  • C only Using designated initializers. The following example uses designated initializers to explicitly initialize only the last four elements of the array. The first eight elements that are not explicitly initialized are automatically initialized to zero.
    static short grid[3] [4] = { [2][0] = 8, [2][1] = 6,
                                 [2][2] = 4, [2][3] = 1 };
    The initial values of grid are:
    Element Value Element Value
    grid[0] [0] 0 grid[1] [2] 0
    grid[0] [1] 0 grid[1] [3] 0
    grid[0] [2] 0 grid[2] [0] 8
    grid[0] [3] 0 grid[2] [1] 6
    grid[1] [0] 0 grid[2] [2] 4
    grid[1] [1] 0 grid[2] [3] 1
    C only