请c语言高手帮忙解释一下!!!

#include "stdio.h"
typedef union{ long x〔2〕;
int y〔4〕;
char z〔8〕;} atx;
typedef struct aa{ long x〔2〕;
int y〔4〕;
char z〔8〕; } stx;
main()
{printf("union=%d,struct aa=%d\n",sizeof(atx),sizeof(stx));}
A)union=8,struct aa=8
B)union=8,struct aa=24
C)union=24,struct aa=8
D)union=24,struct aa=24
正确答案: B
请大家帮忙解释一下!!!谢谢!!!

union 中,所有变量占用的是同一起始地址的内存空间,同一时间只能存在一个;所以总大小就是其中占空间最大的变量。
struct中,内存片段是规范化的,
typedef struct aa{
long x;//8个字节
int y; //8个字节(这个每个编译器都不一样,按题目的意思应该是8个)
char z; //1个字节,规范化填充为8个字节
} stx;
温馨提示:答案为网友推荐,仅供参考
相似回答