6.4. Initialising Arrays
In the above example we initialised the array
hourly_wage by placing a comma separated list of
values in curly braces. Using this method you can initialise as few
or as many array elements as you like however you cannot initialise an
element without initialising all the previous elements. If you
initialise some but not all elements of an array the remaining
elements will be automatically initialised to zero.
To get around this inconvenience, a GNU extension to the C
language allows you to initialise array elements selectively by
number. When initialised by number, the elements can be placed in any
order withing the curly braces preceded by
[index]=value.
Like so:
Example 6-2. initialise_array.c
#include <stdio.h>
int
main()
{
int i;
int first_array[100] = { [90]=4, [0]=5, [98]=6 };
double second_array[5] = { [3] = 1.01, [4] = 1.02 };
printf("sure enough, first_array[90] == %d\n\n", first_array[90]);
printf("sure enough, first_array[99] == %d\n\n", first_array[99]);
for (i = 0; i < 5; i++}
printf("value of second_array[%d] is %f\n", i, second_array[i]);
return 0;
}