18.若有以下的说明和语句,已知int类型占2个字节,则输出结果为( )。
A) 18
B) 20
C) 10
D) 12
#include<stdio.h>
union u
{
int i;
double y;
};
struct s
{
char a[10];
union u h;
};
void main()
{
printf("%d",sizeof(struct s));
}
请问这个输出应该是多少?
结构体合起来,联合体看最长
A
https://www.cnblogs.com/ss-Payne/p/15785028.html
输出应该是24
#include <stdio.h>
// objects of type u must be allocated at 8-byte boundaries
// because u.y must be allocated at 8-byte boundaries
union u
{
int i; // size: 2, alignment: 2 (assuming sizeof(int)=2)
double y; // size: 8, alignment: 8
}; // size: 8, alignment: 8
// objects of type s must be allocated at 8-byte boundaries
// because s.h must be allocated at 8-byte boundaries
struct s
{
char a[10]; // size: 10, alignment: 1
// 6 bytes padding
union u h; // size: 8, alignment: 8
}; // size: 24, alignment: 8
int main()
{
printf("%ld", sizeof(struct s));
}