找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 306|回复: 1

Question2.19:What's the difference between a structure and a union,anyway?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-18 20:48:42 | 显示全部楼层 |阅读模式
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.)     

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-18 21:07:49 | 显示全部楼层
问题:一个结构体和一个联合体的区别是什么?
回答:一个联合体在本质上是一个结构体,只是它们的位域是重叠的;你一次只能使用一个位域(你也可以写入一个位域然后从另一个位域读取,来检测某种类型的二进制模式或者用不同的方式解释他们,当然这些都与机器相关联)。联合体的大小是单个成员中最大的那个所占的大小,而一个结构体的大小则是所有成员大小的总和(这两种情况,空间大小都可能被填充而增加;参考问题2.12和2.13。)。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-2 02:52 , Processed in 0.119238 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表