typedef union A
{
int a[7];
char b;
double c;
};
typedef union B
{
int a;
int b[9];
};
typedef union C
{
double i;
int k[5];
char c;
};
typedef union UB
{
char a;
int c;
char b[9];
};
sizeof(UB)=12;
sizeof(A)=32;
sizeof(B)=36;
sizeof(C)=24;
注意:如果联合中没有double型变量,则联合体的大小是最大的变量所占内存空间的大小,并且还要是除自身外的其它最大成员的整数倍。如果联合体中有double型变量,不仅要以最大的变量为标准还要是8的整数倍,即在最大的变量往上到8的整数倍。
struct hh{
int i;
C c;
char s;
double d;
};
sizeof(hh)=48;4+(4) + 24+ 1+(7) +8=48;
struct S{
int a;
char c;
short s;
};
struct S1{
int a;
short s;
int b;
char c;
};
struct S2{
short s;
int a;
char b;
double c;
};
sizeof(S )= 8; 4+ 1+(1) +2=8;
sizeof(S1)=16; 4+ 2+(2) +4 +1+(3)=16;
sizeof(S2)=24; 2+(2)+4+1+(7)+8=24;