#include <stdio.h>
enum node_type{
t_int,t_double
};
struct node{
enum node_type type;
union{
int t_int;
double t_double;
};
};
int main(void)
{
struct node t;
t.type = t_int;
t.t_int = 2;
printf("The type(t_int:0,t_double:1) is:%d\t And the data is %d\n",t.type,t.t_int);
t.type = t_double;
t.t_double = 2.3f;
printf("The type(t_int:0,t_double:1) is:%d\t And the data is %f\n",t.type,t.t_double);
return 0;
}
用gcc编译器编译不带-std=c99选项可以编译通过,但是带上这个选项后无法通过,可以替换为-std=gnu99,原因是这个匿名的union是gnu c,而不是standard c
现在c11已经添加上了这个匿名union。另外匿名联合(anonymous union)在内核数据结构中用的比较多
问题来源:http://stackoverflow.com/questions/3228104/anonymous-union-within-struct-not-in-c99