Initializing Arrays

  1. An array can be initialized when it is declared by specifying the values of its elements as a comma-separated list.

  2. For example an array can be used to store the x, y, and z-coordinates of a point in 3 dimensions. It can be declared and initialized as follows:

    double xyz_coordinates[3] = {1.2, -3.7, 0.65};

  3. If the list contains fewer values than the size of the array, then the remaining elements will have the value of zero.

  4. When an array is declared and initialized without specifying the array size, then the array will be declared with a size just large enough to store the values supplied by the list.

  5. For example the declaration and initialization:

    int exam_score[ ] = {50, 92, 73};

    is equivalent to the following:

    int exam_score[3] = {50, 92, 73};