Anonymous union within struct not in c99? Anonymous union within struct not in c99? c c

Anonymous union within struct not in c99?


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.


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


Well, the solution was to name instance of the union (which can remain anonymous as datatype) and then use that name as a proxy.

$ diff -u old_us.c us.c --- old_us.c    2010-07-12 13:49:25.000000000 +0200+++ us.c        2010-07-12 13:49:02.000000000 +0200@@ -15,7 +15,7 @@   union {     struct int_node int_n;     struct double_node double_n;-  };+  } data; }; int main(void) {@@ -23,6 +23,6 @@   i.value = 10;   struct node n;   n.type = t_int;-  n.int_n = i;+  n.data.int_n = i;   return 0; }

Now it compiles as c99 without any problems.

$ cc -std=c99 us.c $ 

Note: I am not happy about this solution anyway.