Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C4201 warning in C code

Tags:

c++

c

Will this warning create any problem in runtime?

ext.h(180) : warning C4201: nonstandard extension used : nameless struct/union

like image 287
user1787812 Avatar asked Jan 23 '13 06:01

user1787812


2 Answers

This is when you have a union or struct without a name, e.g.:

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 members of the union as if they were members of the struct. When you give the union a name (which is what the standard requires), you must access a and b through the name of the union instead:

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 an issue as long as you compile your code using compilers that share this extension, it is only a problem when you compile your code using compilers that don't, and because the standard doesn't require this behaviour, a compiler is free to reject this code.

Edit: As pointed out by andyn, C11 explicitly allows this behaviour.

like image 157
dreamlax Avatar answered Sep 30 '22 12:09

dreamlax


Well it is a MSVC warning, which tells you that you are using an compiler specific language extension. so you can check this.

nonstandard extension used : nameless struct/union

Under Microsoft extensions (/Ze), you can specify a structure without a declarator as members of another structure or union. These structures generate an error under ANSI compatibility (/Za).

// C4201.cpp
// compile with: /W4
struct S
{
   float y;
   struct
   {
      int a, b, c;  // C4201
   };
} *p_s;

int main()
{
}

If you are not bothered about portability of your code. i.e: Your target platform is only MSVC then simply ignore the warning.

like image 20
Alok Save Avatar answered Sep 30 '22 11:09

Alok Save