先定义一个结构体
typedef struct __TEST__ {
unsigned char a;
unsigned char b;
} TEST;
int main(void)
{
unsigned char i;
TEST data1[5] = {{1,2},{3,4},{5,6},{7,8},{9,10}};//经常使用的初始化方式
TEST data2[5] ={0};//初始全为0,否则可能是随机数。
data2[0].a = 1;//分别进行初始化,有点麻烦
data2 [0].b = 2;
//省略部分data2的初始化
TEST data3[5] = {[1] = {1,2}, [3] = {3,4}, [4] = {5,6}, [2] = {7,8}, [0] = {9,10}};//首次见到的初始化方法,这样赋值顺序可自定义。但有什么用呢?
for(i = 0 ; i<5;i++)
{
printf("data1[%d]=%d-%dnr",i,data1.a,data1.b);
printf("data2[%d]=%d-%dnr",i,data2.a,data2.b);
printf("data3[%d]=%d-%dnr",i,data3.a,data3.b);
}
}
输出结果:
data1[0]=1-2
data2[0]=1-2
data3[0]=9-10
data1[1]=3-4
data2[1]=0-0
data3[1]=1-2
data1[2]=5-6
data2[2]=0-0
data3[2]=7-8
data1[3]=7-8
data2[3]=0-0
data3[3]=3-4
data1[4]=9-10
data2[4]=0-0
data3[4]=5-6
|