Initializing 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.)

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 are:number1[0] and number1[1] are the same as in the previous definition, but number1[2] is 0.

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.

You can initialize a one-dimensional character array by specifying:

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++ When initializing 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.

You can initialize a multidimensional array using any of the following techniques:

The following definition explicitly initializes six elements in a 12-element array:

static int matrix[3][4] =
   {
     {1, 2},
     {3, 4},
     {5, 6}
   };

The initial values of matrix are shown in the following table. All other elements are initialized to zero.

Element Value Element Value
matrix[0][0] 1 matrix[1][2] 0
matrix[0][1] 2 matrix[1][3] 0
matrix[0][2] 0 matrix[2][0] 5
matrix[0][3] 0 matrix[2][1] 6
matrix[1][0] 3 matrix[2][2] 0
matrix[1][1] 4 matrix[2][3] 0

Related References

IBM Copyright 2003