数组指定初始化

array_s array[5] = {
  [1] = {x, {x, x}},
  [3] = {x, x, x},
};
#include <stdio.h>

typedef struct {
    int b1;
    int b2;
} B_type_s;

typedef struct {
  int a;
  B_type_s b;
} array_s;

array_s array[5] = {
  [1] = {10, {20, 30}},
  [3] = {40, 50, 60},
};

int main()
{
    int i;

    printf("\t\ta\t b.b1\t b.b2\n");
    for(i = 0; i < 5; i++)
        printf("array[%d]\t %d\t %d\t %d\n", i, array[i].a, array[i].b.b1, array[i].b.b2);
   
    return 0;
}
运行结果:
		a	 b.b1	 b.b2
array[0]	 0	 0	 0
array[1]	 10	 20	 30
array[2]	 0	 0	 0
array[3]	 40	 50	 60
array[4]	 0	 0	 0

C在线编译器

posted @ 2021-06-04 16:28  Bryce-Hu  阅读(123)  评论(0)    收藏  举报