union (共用体)与 struct (结构体)的区别
#include "cstdio"
#include "cstdlib"
struct mim {
char a;
short b;
int c;
};
union mi {
char a;
short b;
int c;
};
int main() {
union mi tmp;
tmp.a = 0x11;
tmp.c = 0x22222222;
tmp.b = 0x1234;
printf("%x\n", tmp.a);
printf("%x\n", tmp.b);
printf("%x\n", tmp.c);
printf("所占空间大小为%d个字节\n", sizeof(tmp));
struct mim tmp2;
tmp2.a = 0x11;
tmp2.c = 0x22222222;
tmp2.b = 0x1234;
printf("%x\n", tmp2.a);
printf("%x\n", tmp2.b);
printf("%x\n", tmp2.c);
printf("所占空间大小%d个字节\n", sizeof(tmp2));
system("pause");
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/97fcf5d66cb0ea7565a88b1c0ff0dfad.png)