Warning C4201 in C code
This is when you have a union or structure with no name, for example:
typedef struct
{
union
{
int a;
int b;
}; // no name
int c;
} MyStruct;
MyStruct m;
m.a = 4;
m.b = 6; //overwrites m.a
m.c = 8;
It allows you to access union members as if they were members of a structure. When you give a union a name (which is what the standard requires), you should access a
and b
instead of the union name:
typedef struct
{
union
{
int a;
int b;
} u;
int c;
}
MyStruct m;
m.u.a = 4;
m.u.b = 6; // overwrites m.u.a
m.c = 8;
It is not a problem if you compile your code with compilers that use this extension, it is only a problem when you compile your code with compilers that do not, and since the standard does not require this behavior, the compiler may reject this code.
Edit: As andyn pointed out, C11 explicitly allows this behavior.
source to share
Well, this is an MSVC warning saying that you are using a compiler-specific language extension. so you can check it out .
non-standard extension used: nameless struct / union
In Microsoft Extensions (/ Ze), you can specify a structure without a declarator as members of another structure or union. These structures generate an ANSI compatibility error (/ Za).
// C4201.cpp
// compile with: /W4
struct S
{
float y;
struct
{
int a, b, c; // C4201
};
} *p_s;
int main()
{
}
If you are not worried about the portability of your code. ie: Your target platform is MSVC only and then just ignores the warning.
source to share