Array specified initialization in GNU C

In GNU C, arrays support designated initialization. Simply put, when initializing arrays and structures, you can assign initial values ​​by specifying array subscripts or specific member names.
For example, the following initialization is supported:

int array[6] = { [4] = 29, [2] = 15 }; // 可以不按顺序赋值

It also supports assigning the same value to the elements in the specified range in the array:

int array[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };

Using this feature, we can initialize the array while applying, such as clearing all of them:

int array[NR_MAX + 1] = {[0 ... NR_MAX] = 0}

This is equivalent to calling memset(array, 0, sizeof(array)), comparing the performance, and finding that it is basically the same.
 

Guess you like

Origin blog.csdn.net/choumin/article/details/111499540