C 编程:初始化联合数组

C Programming: Initialize Array of Unions

我有一个联合定义为:

typedef union{
    unsigned b_a: 1;
    unsigned b_b:1;
    unsigned b_c:1;
    int d_e;
    int option;

    int data[7];
}myUnion;

我想声明并初始化一个 myUnion 类型的数组:

myUnion myUnionArray[1] = { {.b_a=0, .b_b=0, .b_c=0, .d_e=5, .option=6} }

我已经尝试用上面的行来初始化数组,但在编译时我总是出错 ("too many initializers")。

知道我做错了什么吗?

我猜,你对 union 和 struct 感到困惑。

Union - union is type whose members overlaps the storage and size of the union is the max size of any member. Hence you cannot initialize multiple simultaneously.

请找出struct and union
之间的区别 如果对嵌套初始化感兴趣,请关注this
如果对数组的初始化感兴趣,请关注this