匿名union

#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

转载于:https://www.cnblogs.com/linghuchong0605/p/3665661.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值