我有一个 union 声明:
union test_u
{
int i;
char *str;
};
我正在尝试用“第二”字段中的数据初始化一个变量,使用代码:
union test_u test = {"Sample"}; // char *, not int
尝试编译时,我收到错误:
file.c:72:11: warning: initialization makes integer from pointer without a cast
是否可以按照我上面的方式初始化变量?编译器(在 C89 下)不应该在初始化时接受 int
到 char *
吗?
最佳答案
在 C99 中,可以使用 designated initialisers :
union test_u test = { .str = "Sample" };
关于c - 无法初始化 union 中的第二项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10923178/