Error: Redefinition of union
c, compiler-errors, struct, unions
Solution
The name 'U' in your example is the name of the union union, while 'U1' is the name of the instance of the union within each of the structs 'A' and 'B'. U1 can be re-used but the name 'U' has scope throughout the file.
You could have done this:
typedef struct
{
union U
{
int a;
char b;
}U1;
}A;
typedef struct
{
union U U1;
}B;
since both A.U1 and B.U1 have the same declaration. Otherwise, you'd have to use different names for the union.
Or, just use anonymous unions (ie. do not provide a name for the union at all):
typedef struct
{
union
{
int a;
char b;
}U1;
}A;
typedef struct
{
union
{
int a;
char b;
}U1;
}B;
Problem
In the following code: ``` typedef struct { union U { int a; char b; }U1; }A; typedef struct { union U { int a; char b; }U1; }B; ``` The compiler gives an error "[Error] redefinition of 'union U' ". But these unions are members of different structures. So generally there is no chance of variable name interference. So what can be the reason for this error?