联合结构体的大小

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;




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值