When we initialize an array like this int a[5] = {0} , the compiler does all 5 elements of 0. This is a really nice, compact initialization and a useful function.
But I wonder why the compiler does not initialize int a[5]={1} same way? Why doesn't he do all 5 elements 1? Why does the standard not provide for this? Is this not surprising? Is it not lost?
In addition, if the number of elements in the initializer is less than the size of the array, then compilation can initialize the remaining elements with the last element in the initializer. Values int a[5]={1,2,3} equivalent to int a[5]={1,2,3,3,3} . Similarly, int a[10]={1,2,3,0} equivalent to int a[10]={1,2,3,0,0,0,0,0,0,0}; .
Wouldn't all this be an amazing feature if the Standard empowers it? Or are there good reasons for this missing feature?
And there is something called a designated initializer on C99 that is used as:
Designated initializers can be combined with regular initializers, in the following example:
int a[10] = {2, 4, [8]=9, 10}
In this example, [0] is initialized to 2, a 1 is initialized to 4, and [2] - [7] are initialized to 0, and [9] is initialized to 10.
Quite interesting. But even this function is not in C ++.