Pointers on C——10 Structures and Unions.14

10.6 Unions


A union is a different animal altogether. A union is declared like a structure but doesnʹt work like a structure. All of the members of a union refer to the same location(s)in memory. Unions are used when you need to store different things in one place at different times.

和结构相比,联合(union) 可以说是另一种动物了。联合的声明和结构类似,但它的行为方式却和结构不同。联合的所有成员引用的是内存中的相同位置。当你想在不同的时刻把不同的东西存储于同一个位置时,就可以使用联合。


First, letʹs look at a simple example.

首先,让我们看一个简单的例子。


union {

float f;

int i;

} fi;


On a machine in which floats and integers both occupy 32 bits, the variable fi occupies only one 32‐bit word of memory. If the member f is used, the word is accessed as a floating‐point value; if the member i is used, the word is accessed as an integer. So this code

在一个浮点型和整型都是32 位的机器上,变量fi只占据内存中一个32 位的字。如果成员f 被使用,这个字就作为浮点值访问:如果成员i 被使用,这个字就作为整型值访问。所以,下面这段代码


fi.f = 3.14159;

printf( "%d\n", fi.i );


first stores the floating‐point representation of π into fi, and then interprets those same bits as if they were an integer and prints out that value. Note that both member are referring to the same bits; the only difference is that the type of each member determines how the bits are interpreted.

首先把π 的浮点表示形式存储于fi,然后把这些相同的位当作一个整型值打印输出。注意这两个成员所引用的位相同,仅有的区别在于每个成员的类型决定了这些位被如何解释。


Why would anyone ever want to do anything like this example? It would be helpful if you wanted to see how floating‐point numbers are stored on a particular machine but probably not for anything else. Here is a more realistic example. One task of a BASIC interpreter is to keep track of the values of variables used in. the program.

为什么人们有时想使用类似此例的形式呢?如果你想看看浮点数是如何存储在一种特定的机器中但又对其他东西不感兴趣,联合就可能有所帮助。这里有一个更为现实的例子。BASIC 解释器的任务之一就是记住程序所使用的变量的值。


BASIC provides several different types of variables, so the type of each variable must be stored along with its value. Here is a structure that saves this information, but it is not too efficient.

BASIC 提供了几种不同类型的变量,所以每个变量的类型必须和它的值一起存储。这里有一个结构,用于保存这个信息,但它的效率不高。


struct VARIABLE{

enum { INT, FLOAT, STRING } type;

int int_value;

float float_value;

char *string_value;

};


When a variable in the BASIC program is created, the interpreter creates one of these structures and records the type of the variable. Then, based on the type, one of the three value fields is used to store the variableʹs value.

当BASIC 程序中的一个变量被创建时,解释器就创建一个这样的结构并记录变量的类型。然后,根据变量的类型,把变量的值存储在这三个值字段的其中一个。


What is inefficient about this structure is the amount of memory used— every VARIABLE contains two value fields that are not used. A union can reduce this waste by storing each of the three value fields in the same memory. The three fields will not conflict because each variable can only have one type, thus only one of the fields in the union will ever be needed at a time.

这个结构的低效之处在于它所占用的内存——每个VARIABLE 结构存在两个未使用的值字段。联合就可以减少这种浪费,它把这三个值字段的每一个都存储于同一个内存位置。这三个字段并不会冲突,因为每个变量只可能具有一种类型,这样在某一时刻,联合的这几个字段只有一个被使用。


struct VARIABLE{

enum { INT, FLOAT, STRING } type;

union {

int i;

float f;

char *s;

} value;

};


Now, for an integer variable, you would store INT in the type field and the integer value in the value.i field. For a floating‐point value, you would use the value.f field.

现在,对于整型变量,你将在type 字段设置为INT ,并把整型值存储于value.i字段。对于浮点值,你将使用value.f 字段。


When obtaining the value of this variable later, the type field would be checked to determine which value field to use. This choice determines how the memory location will be accessed, so one location can be used to store any of these three different kinds of values. Note that the compiler doesnʹt check the type field to verify that the proper union member is used. It is up to the programmer to maintain the type field and to check it.

当以后得到这个变量的值时,对type 字段进行检查决定使用哪个值字段。这个选择决定内存位置如何被访问,所以同一个位置可以用于存储这三种不同类型的值。注意编译器并不对type 字段进行检查证实程序使用的是正确的联合成员。维护并检查type 字段是程序员的责任。


If the member of a union are different sizes, the union will be as large as the largest member. The next section discusses this situation.

如果联合的各个成员具有不同的长度,联合的长度就是它最长成员的长度。下一节将讨论这种情况。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值