#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//联合体 共享空间 内存占用按照成员最大的地址sizeof(int)
typedef union test {
char ch;
short sh;
int a;
}test_t;
int main(int argc, char* argv[])
{
test_t obj;
obj.a = 0x87654321;
//地址相同
printf("&obj.ch = %p\n&obj.sh = %p\n&obj.a = %p\n", &obj.ch, &obj.sh, &obj.a);
//访问到的值不同
printf("obj.ch = %x\nobj.sh = %x\nobj.a = %x\n", obj.ch, obj.sh, obj.a);
system("pause");
return EXIT_SUCCESS;
}
05-12
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-21
136
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-21
140
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-03
1766
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-24
788
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)