C语言编程中的联合体union入门学习教程

2020-01-06 14:13:14王旭

测试程序:


/*测试联合体 2011.10.3*/

#include <iostream>
using namespace std;

union U1
{
  char s[9];
  int n;
  double d;
};

union U2
{
  char s[5];
  int n;
  double d;
};

int main(int argc, char *argv[])
{
  U1 u1;
  U2 u2;
  printf("%dn",sizeof(u1));
  printf("%dn",sizeof(u2));
  printf("0x%xn",&u1);
  printf("0x%xn",&u1.s);
  printf("0x%xn",&u1.n);
  printf("0x%xn",&u1.d);
  u1.n=1;
  printf("%dn",u1.s[0]);
  printf("%lfn",u1.d);
  unsigned char *p=(unsigned char *)&u1;
  printf("%dn",*p);
  printf("%dn",*(p+1));
  printf("%dn",*(p+2));
  printf("%dn",*(p+3));
  printf("%dn",*(p+4));
  printf("%dn",*(p+5));
  printf("%dn",*(p+6));
  printf("%dn",*(p+7));
  return 0;
}

输出结果为:


16
8
0x22ff60
0x22ff60
0x22ff60
0x22ff60
1
0.000000
1
0
0
0
48
204
64
0
请按任意键继续. . .

对于sizeof(u1)=16。因为u1中s占9字节,n占4字节,d占8字节,因此至少需要9字节。其包含的基本数据类型为char,int,double分别占1,4,8字节,为了使u1所占空间的大小能被1,4,8整除,则需填充字节以到16,因此sizeof(u1)=16.

对于sizeof(u2)=8。因为u2中s占5字节,n占4字节,d占8字节,因此至少需要8字节。其包含的基本数据类型为char,int,double分别占1,4,8字节,为了使u2所占空间的大小能被1,4,8整除,不需填充字节,因为8本身就能满足要求。因此sizeof(u2)=8。

从打印出的每个成员的基地址可以看出,联合体中每个成员的基地址都相同,等于联合体变量的首地址。

对u1.n=1,将u1的n赋值为1后,则该段内存的前4个字节存储的数据为00000001 00000000 00000000 00000000

因此取s[0]的数据表示取第一个单元的数据,其整型值为1,所以打印出的结果为1.

至于打印出的d为0.000000愿意如下。由于已知该段内存前4字节的单元存储的数据为00000001 00000000 00000000 00000000,从上面打印结果48,204,64,0可以知道后面4个字节单元中的数据为00110000 11001100 01000000 00000000,因此其表示的二进 制浮点数为

00000000 01000000 11001100 00110000 00000000 00000000 00000000 00000001

对于double型数据,第63位0为符号位,62-52 00000000100为阶码,0000 11001100 00110000 00000000 00000000 00000000 00000001为尾数,根据其值知道尾数值约为0,而阶码为4-1023=-1019,因此其表示的浮点数为1.0*2^(-1019)=0.00000000000......,因此输出结果为0.000000。