Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous union within struct not in c99?

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.

like image 754
Martin Avatar asked Jul 12 '10 11:07

Martin


People also ask

Can we declare union inside structure?

A structure can be nested inside a union and it is called union of structures. It is possible to create a union inside a structure.

How do you use an anonymous union?

An anonymous union is not a type; it defines an unnamed object. The member names of an anonymous union must be distinct from other names within the scope in which the union is declared. You can use member names directly in the union scope without any additional member access syntax.

What is an anonymous struct?

An anonymous struct declaration is a declaration that declares neither a tag for the struct, nor an object or typedef name. Anonymous structs are not allowed in C++. The -features=extensions option allows the use of an anonymous struct declaration, but only as member of a union.

How is union inside structure defined?

A union is a user-defined type similar to structs in C except for one key difference. Structures allocate enough space to store all their members, whereas unions can only hold one member value at a time.


2 Answers

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.

like image 162
R.. GitHub STOP HELPING ICE Avatar answered Sep 24 '22 06:09

R.. GitHub STOP HELPING ICE


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).

like image 31
bk. Avatar answered Sep 22 '22 06:09

bk.