Consider the following two examples:
1.
union test{
struct {
int a;
int b[];
};
};
int main(void){
union test test;
test.a = 10;
printf("test.b[0] = %d", test.b[0]); //prints 0, UB?
}
2.
#include <stdio.h>
union test{
int a;
int b[]; //error: flexible array member in union
};
int main(void){
union test test;
test.a = 10;
printf("test.b[0] = %d", test.b[0]);
}
The behavior is unclear. I expected the examples would behave the same (i.e. the first example would also fail to compile) since 6.7.2.1(p13):
The members of an anonymous structure or union are considered to be members of the containing structure or union.
So I interpreted the wording as if a union contains an anonymous struct as a member the members of the anonymous struct would be considered as members of the containing union.
Question: Why does the first example compile fine instead of failing as the second one?