• 3204阅读
  • 1回复

Question2.19:What's the difference between a structure and a union,anyway? [复制链接]

上一主题 下一主题
离线那个谁
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2013-07-18
— 本帖被 tyrone2497谁 执行加亮操作(2013-07-19) —
Q: What's the difference between a structure and a union, anyway?
A: A union is essentially a structure in which all of the fields overlay each other; you can only use one field at a time. (You can also cheat by writing to one field and reading from another, to inspect a type's bit patterns or interpret them differently, but that's obviously pretty machine-dependent.) The size of a union is the maximum of the sizes of its individual members, while the size of a structure is the sum of the sizes of its members. (In both cases, the size may be increased by padding; see questions 2.12 and 2.13.)    
离线那个谁

只看该作者 沙发  发表于: 2013-07-18
问题:一个结构体和一个联合体的区别是什么?
回答:一个联合体在本质上是一个结构体,只是它们的位域是重叠的;你一次只能使用一个位域(你也可以写入一个位域然后从另一个位域读取,来检测某种类型的二进制模式或者用不同的方式解释他们,当然这些都与机器相关联)。联合体的大小是单个成员中最大的那个所占的大小,而一个结构体的大小则是所有成员大小的总和(这两种情况,空间大小都可能被填充而增加;参考问题2.12和2.13。)。
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个