- #include <stdio.h>
-
- int array[10] =
- {
- [0 ... 9] = 0,
- [8] = 8,
- [2 ... 2] = 2,
- [5 ... 7] = 5
- };
-
- int main()
- {
- int i = 0;
-
- for (i = 0; i < 10; i++)
- printf("%d/n", array[i]);
- return 0;
- }
Output:
0
0
2
0
0
5
5
5
8
0
- #include <stdio.h>
- struct StObErrorStringMap
- {
- const char *jdbc_state;
- const char *odbc_state;
- };
-
- struct StObErrorStringMap arr[10] =
- {
- [0 ...9] = {(const char*)"HY000", (const char*)"S1000"},
- [1] = { (const char*)"HY001", (const char*)"S1000" },
- };
-
- int main()
- {
- int i = 0;
-
- for (i = 0; i < 10; i++)
- printf("%s\n", arr[i].jdbc_state);
- return 0;
- }
总结:
1. gcc的数组初始化中,任意元素都能够被重复初始化,并且以最后一次初始化值为准。
2. 初始化单个元素的方法有两种 [index] = value, [index ... index] = value.
3. 实验表明,如果index超出了数组最大下标,编译器报错。
阅读(2126) | 评论(0) | 转发(0) |