C函数数组元素初始化

初始化时,可随意指定初始化的元素或者元素的范围。

附gnu c 手册。

http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

代码:

test.c

 1 #include <stdio.h>
 2 static const unsigned int boot_gdt[] __attribute__((aligned(16))) = {
 3         [1] = 0x11223344,
 4         [2] = 0x11223344,
 5         [3 ... 10] = 0x32,
 6         [15] = 0x11223344
 7 };
 8 
 9 typedef struct { unsigned long int pte; } pte_t;
10 
11 int main(void){
12     int len = sizeof(boot_gdt)/sizeof(int) - 1;
13     printf("the len of  boot_gdt is %d 
", len + 1);
14     for(int i = 0; i <= len; i++){
15         printf("the %d data is 0x%x
", i, boot_gdt[i]);
16     }
17 }

http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays 

编译:

$ gcc -o test test.c -std=c99

运行:

$ ./test
the len of  boot_gdt is 16
the 0 data is 0x0
the 1 data is 0x11223344
the 2 data is 0x11223344
the 3 data is 0x32
the 4 data is 0x32
the 5 data is 0x32
the 6 data is 0x32
the 7 data is 0x32
the 8 data is 0x32
the 9 data is 0x32
the 10 data is 0x32
the 11 data is 0x0
the 12 data is 0x0
the 13 data is 0x0
the 14 data is 0x0
the 15 data is 0x11223344

原文地址:https://www.cnblogs.com/shaohef/p/3872909.html