小编Joe*_*oel的帖子

联盟中的概念问题

我的代码是这样的

// using_a_union.cpp
#include <stdio.h>

union NumericType
{
    int         iValue;
    long        lValue;  
    double      dValue;  
};

int main()
{
    union NumericType Values = { 10 };   // iValue = 10
    printf("%d\n", Values.iValue);
    Values.dValue = 3.1416;
    printf("%d\n", Values.iValue); // garbage value
}
Run Code Online (Sandbox Code Playgroud)

在我尝试打印Values.iValue后,为什么会获得垃圾值Values.dValue = 3.1416?我以为内存布局会像这样.会发生什么Values.iValue,并 Values.lValue;当我给你的东西Values.dValue

c c++ unions

8
推荐指数
2
解决办法
908
查看次数

标签 统计

c ×1

c++ ×1

unions ×1