目录
2.在上下文及头文件均正常的情况下,执行下面语句后的输出为()
1.有变量
struct{
char type;
int num;
char name[10];
}rec;
char是1字节,int是4字节,若按4字节对齐,那么sizeof(rec)等于()
答案:20
解析:
struct {
char type; ===>占一个字节
int num; ===>占四个字节,但考虑字节对齐,int num不会紧跟着type,对齐的最终结果是char type和int num共占8字节
char name[10]; ===>虽然数组个数是10个,占10个字节,但考虑字节对齐,最后回对齐到12字节,所以该结构体总占字节为8+12 = 20
} rec;