结构初始化器中的匿名联合
Anonymous Union in Struct Initializer
为什么以下示例在 C 中不起作用?
#include <stdio.h>
typedef struct {
int x;
} X;
typedef struct {
char y[10];
} Y;
typedef struct {
int pos;
union {
X x;
Y y;
};
} Test;
int main() {
X x = { 65 };
Y y = { "Hallo" };
Test t = { 1, x }; // OK
printf("1: %d %d '%s'\n", t.pos, t.x.x, t.y.y);
Test t2 = { 2, y }; // ERROR
printf("2: %d %d '%s'\n", t2.pos, t2.x.x, t2.y.y);
Test t3 = { 3 }; // OK
printf("3: %d %d '%s'\n", t3.pos, t3.x.x, t3.y.y);
return 0;
}
main.c: In function ‘main’:
main.c:25:3: error: incompatible types when initializing type ‘int’ using type ‘Y’
Test t2 = { 2, y }; // ERROR
^
编辑:
顺便说一句:t2.y = y;
有效
因为未针对 union
.
的可能成员分析和匹配初始化程序的类型
相反,您只需为第一个 union
成员提供一个初始化程序。
C11 草案 §6.7.9.17:
Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.
如前所述,您可以使用指示符来控制它:
Test t2 = { 2, .y = y };
应该可以工作(.y
是一个指示符,在 C99 中是 "new")。
您必须指定要初始化的成员不是第一个:
Test t2 = { 2, { .y = y } } ;
否则编译器将尝试初始化它,就好像您这样写:.x = y
为什么以下示例在 C 中不起作用?
#include <stdio.h>
typedef struct {
int x;
} X;
typedef struct {
char y[10];
} Y;
typedef struct {
int pos;
union {
X x;
Y y;
};
} Test;
int main() {
X x = { 65 };
Y y = { "Hallo" };
Test t = { 1, x }; // OK
printf("1: %d %d '%s'\n", t.pos, t.x.x, t.y.y);
Test t2 = { 2, y }; // ERROR
printf("2: %d %d '%s'\n", t2.pos, t2.x.x, t2.y.y);
Test t3 = { 3 }; // OK
printf("3: %d %d '%s'\n", t3.pos, t3.x.x, t3.y.y);
return 0;
}
main.c: In function ‘main’:
main.c:25:3: error: incompatible types when initializing type ‘int’ using type ‘Y’
Test t2 = { 2, y }; // ERROR
^
编辑:
顺便说一句:t2.y = y;
有效
因为未针对 union
.
相反,您只需为第一个 union
成员提供一个初始化程序。
C11 草案 §6.7.9.17:
Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.
如前所述,您可以使用指示符来控制它:
Test t2 = { 2, .y = y };
应该可以工作(.y
是一个指示符,在 C99 中是 "new")。
您必须指定要初始化的成员不是第一个:
Test t2 = { 2, { .y = y } } ;
否则编译器将尝试初始化它,就好像您这样写:.x = y