Warning C4201 in C code

Will this warning create any runtime problems?

ext.h (180): warning C4201: non-standard extension used: nameless struct / union

+3


source to share


5 answers


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.

+7


source


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.

+1


source


No, it won't create any problems, it just means that your code is not standards-compliant, which means that it cannot compile with some other compilers.

0


source


As long as it's supported by the compiler, it should work fine until you decide to connect to a new platform (or use a new compiler) or drop support.

0


source


You probably have the same problem as.

Just provide a name for your structures / unions

eg.

struct mystruct {
   ...
} 

      

0


source







All Articles