结构体中的匿名联合不在 c99 中?
这是我遇到的问题的非常简化的代码:
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; }
我不明白的是:
$ 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’
使用不带 -std
选项的 GCC
编译上面的代码没有任何问题(以及类似的代码工作得很好),但似乎c99
不允许这种技术。为什么会这样?是否可以兼容 c99
(或 c89
、c90
)?谢谢。
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’
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Union 必须有一个名称并声明如下:
Union must have a name and be declared like this:
另一个解决方案是将公共标头值(
enum node_type type
)放入每个结构中,并使顶级结构成为联合。这并不完全是“不要重复自己”,但它确实避免了匿名联合和看起来不舒服的代理值。Another solution is to put the common header value (
enum node_type type
) into every structure, and make your top-level structure a union. It's not exactly "Don't Repeat Yourself", but it does avoid both anonymous unions and uncomfortable looking proxy values.查看 C99 的 6.2.7.1,我发现标识符是可选的:
我一直在上下搜索,但找不到任何违反规范的匿名联合的引用。整个 -opt 后缀表示该事物(在本例中为
identifier
)根据 6.1 是可选的。Looking at 6.2.7.1 of C99, I'm seeing that the identifier is optional:
I've been up and down searching, and cannot find any reference to anonymous unions being against the spec. The whole -opt suffix indicates that the thing, in this case
identifier
is optional according to 6.1.匿名联合是 GNU 扩展,不属于任何 C 语言标准版本的一部分。您可以对 c99+GNU 扩展使用 -std=gnu99 或类似的东西,但最好编写正确的 C,而不是依赖于只提供语法糖的扩展...
编辑: 匿名联合是添加到 C11 中,因此它们现在已成为该语言的标准部分。大概 GCC 的
-std=c11
允许您使用它们。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...
Edit: 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 标准中,但它们在 C11 标准中。 GCC 和 clang 已经支持这一点(C11 标准似乎已经从 Microsoft 中取消了该功能,并且 GCC 已经为某些 MSFT 扩展提供了一段时间的支持)。
I'm finding this question about a year and a half after everybody else did, so I can give a different answer: anonymous structs are not in the C99 standard, but they are in the C11 standard. GCC and clang already support this (the C11 standard seems to have lifted the feature from Microsoft, and GCC has provided support for some MSFT extensions for some time).
好吧,解决方案是命名联合实例(可以作为数据类型保持匿名),然后使用该名称作为代理。
现在它编译为
c99
没有任何问题。注意:无论如何,我对这个解决方案并不满意。
Well, the solution was to name instance of the union (which can remain anonymous as datatype) and then use that name as a proxy.
Now it compiles as
c99
without any problems.Note: I am not happy about this solution anyway.
只是为了澄清匿名
struct
或匿名union
。C11
6.7.2.1 结构和联合说明符
C99 没有匿名结构体或联合体
简化:类型说明符 标识符
{
声明列表}< /code> 标签
;
struct
或union
;struct
或union
的自定义名称;struct
和匿名union
typedef
,则标签是别名,而不是标签。仅当它没有标识符和标签,并且存在于另一个
struct
或union< 内时,它才是匿名
struct
或匿名union
/代码>。typedef
地狱:如果你有一个typedef
标签部分就不再是标签了,它是该类型的别名。下面的例子只是将
struct
更改为union
,工作方式相同。Just for clarifications about anonymous
struct
or anonymousunion
.C11
6.7.2.1 Structure and union specifiers
C99 There are no anonymous struct or union
Simplified: Type-specifier Identifier
{
Declaration-list}
Tags;
struct
orunion
;struct
orunion
;struct
and anonymousunion
typedef
in front of the Type-specifier, the Tags are alias and not Tags.It is a anonymous
struct
or anonymousunion
only if it have no identifier and no tag, and exist inside anotherstruct
orunion
.typedef
hell: if you have atypedef
the tag part is not a tag anymore, it is alias for that type.The example bellow just change
struct
forunion
, work the same way.