Initializing Arrays
-
An array can be initialized when it is declared by specifying the values of its elements as a comma-separated list.
-
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};
-
If the list contains fewer values than the size of the array, then the remaining elements will be left uninitialized.
-
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.
-
For example the declaration and initialization:
int exam_score[ ] = {50, 92, 73};
is equivalent to the following:
int exam_score[3] = {50, 92, 73};