c++ - C 代码中的 C4201 警告

标签 c++ c

此警告会在运行时产生任何问题吗?

ext.h(180):警告 C4201:使用了非标准扩展:无名结构/union

最佳答案

这是当你有一个没有名字的 union 或结构时,例如:

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;

它允许您像访问结构成员一样访问 union 成员。当您为 union 命名(这是标准要求的)时,您必须改为通过 union 名称访问 ab:

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;

只要您使用共享此扩展的编译器编译代码,这不是问题,只有当您使用的编译器编译代码时,这才是问题,因为标准不需要这种行为,编译器可以自由地拒绝这种代码。

编辑:正如 andyn 所指出的,C11 明确允许这种行为。

关于c++ - C 代码中的 C4201 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14473420/

相关文章:

c - Clang 5.0 上的 vsprintf 和 vsnprintf [-Wformat-nonliteral] 警告

C++:将类传递给 vararg 函数

c# - 来自 native 应用程序的 CoCreateInstance C# COM 组件找不到引用

c++ - "Unable to load DLL ... The specified procedure could not be found"Windows XP 上的 C++

c++ - 为什么在链接时不解析共享库的符号?

c - 从多线程应用程序生成进程

c++ - 第一次尝试创建链表

c++ - 无法调整作为类成员变量的 C++ std::vector 的大小

c++ - 错误 : non-static reference member 'std::ostream& Student::out' , 无法使用默认赋值运算符

c++ - 如何在 Windows 上使用 cproto