相关疑难解决方法(0)

在union中使用char数组

我能够打印出int的地址和值,但不能打印出union的字符.为什么会这样

#include <iostream>

using namespace std;

union Endian
{
    int i;
    char c[sizeof(int)];
    int j;
};

int main(int argc, char *argv[]) {
    Endian e;
    e.i = 20;
    cout << &e.j;
    cout << &e.i;
    cout << &e.c[0]; //Why can't I print this address
    cout << e.c[1]; // Why can't I print this value

}
Run Code Online (Sandbox Code Playgroud)

O/P:0x7fff5451ab68 0x7fff5451ab68

c++

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1