结构内不C99匿名工会吗? [英] Anonymous union within struct not in c99?

查看:213
本文介绍了结构内不C99匿名工会吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题这里很简单code我有:

here is very simplified code of problem I have:


enum node_type {
    t_int, t_double
};

struct int_node {
    int value;
};

struct double_node {
    double value;
};

struct node {
    enum node_type type;
    union {
        struct int_node int_n;
        struct double_node double_n;
    };
};

int main(void) {
    struct int_node i;
    i.value = 10;
    struct node n;
    n.type = t_int;
    n.int_n = i;
    return 0;
}

和我不为已了解这一点:

And what I don't undestand is this:


$ cc us.c 
$ cc -std=c99 us.c 
us.c:18:4: warning: declaration does not declare anything
us.c: In function ‘main’:
us.c:26:4: error: ‘struct node’ has no member named ‘int_n’

使用 GCC 没有 -std 选项编译code以上没有任何问题(以及类似code正在pretty好),但似乎 C99 不允许这种技术。为什么会这样,是有可能使为 C99 (或 C89 C90 )兼容?谢谢你。

Using GCC without -std option compiles code above without any problems (and the similar code is working pretty well), but it seems that c99 does not permit this technique. Why is it so and is it possible to make is c99 (or c89, c90) compatible? Thanks.

推荐答案

匿名工会是一个GNU扩展,而不是C语言的任何标准版本的一部分。您可以使用-std = gnu99或类似的东西对C99 + GNU扩展,但它是最好写正确的C和不依赖于它提供什么,但语法糖的扩展...

Anonymous unions are a GNU extension, not part of any standard version of the C language. You can use -std=gnu99 or something like that for c99+GNU extensions, but it's best to write proper C and not rely on extensions which provide nothing but syntactic sugar...

编辑:C11中添加匿名联合,所以他们现在语言的标准组成部分。 presumably GCC的 -std = C11 ,您可以使用它们。

Anonymous unions were added in C11, so they are now a standard part of the language. Presumably GCC's -std=c11 lets you use them.

这篇关于结构内不C99匿名工会吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆