1、union体所有变量共享内存
2、union体从低位开始存储变量
3、union体内存大小为最大变量所占内存(包括数组)
#include<iostream>
using namespace std;
union
{
short A;
char B[3];
char C;
}c;
int main()
{
c.B[0] = 'a';//初始化union的内存空间
c.B[1] = 'b';
cout << c.B[0] << endl;
cout << c.B[1] << endl;
//从起始地址开始类型转换加解引用(之后的一个short字节,即1个字节)
cout << *((short *)&c.B[0]) << endl;
//从offset=1字节开始类型转换加解引用(之后的一个short字节,即1个字节)
cout << *((short *)&c.B[1]) << endl;
//从offset=1字节开始类型转换加解引用(之后的一个int字节,即4个字节)
cout << *((int *)&c.B[1]) << endl;
cout << c.A << endl;
cout << c.C << endl;
system("pause");
return 0;
}