user1787812
user1787812

Reputation: 551

C4201 warning in C code

Will this warning create any problem in runtime?

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

Upvotes: 7

Views: 5183

Answers (5)

dreamlax
dreamlax

Reputation: 95335

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.

Upvotes: 12

Déjà vu
Déjà vu

Reputation: 28830

You probably have the same problem as him

Just give a name to your structs / unions

E.g.

struct mystruct {
   ...
} 

Upvotes: 0

Karthik T
Karthik T

Reputation: 31952

As long as it is compiler supported it should work ok until you decide to port to a new platform (or use a new compiler), or they withdraw support.

Upvotes: 0

wich
wich

Reputation: 17127

No it will not create any problem, it just means that your code is not standards compliant, which means that it might not compile with some other compilers.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206526

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.

Upvotes: 1

Related Questions