### Initializing Arrays

The initializer for an array contains the = symbol followed by a comma-separated list of constant expressions enclosed in braces ({ }). You do not need to initialize all elements in an array. Elements that are not initialized (in extern and static definitions only) receive the value 0 of the appropriate type. ...

The following show four different character array initializations:

    static char name1[] = { 'J', 'a', 'n' };
    static char name2[] = { "Jan" };
    static char name3[3] = "Jan";
    static char name4[4] = "Jan";

These initializations create the following elements:

Element | Value | Element | Value | Element | Value | Element | Value
name1[0] | J | name2[0] | J | name3[0] | J | name4[0] | J
name1[1] | a | name2[1] | a | name3[1] | a | name4[1] | a
name1[2] | n | name2[2] | n | name3[2] | n | name4[2] | n
|  | name2[3] | \0 |  |  | name4[3] | \0

Note that the NULL character is lost for name1[] and name3 [3]. In C, a compiler warning is issued for name3[3 ]. In C++, the compiler issues a severe error for name3[3 ].
Initializing a multidimensional array

Initialize a multidimensional array by:

- 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 a s 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 use nested braces to initialize dimensions and elements in a dimension selectively.

You cannot have more initializers than the number of elements in the array.

Array
Initializer