结构/联合体中的冒号问题
[ 2007-5-14 6:47:00 | By: CANopen ]
 

Q:I came across some structure declarations with colons and numbers next to certain fields, like this:

struct record {
	char *name;
	int refcount : 4;
	unsigned dirty : 1;
};
What gives?


A:Those are bit-fields; the number gives the exact size of the field, in bits. (See any complete book on C for the details.) Bit-fields can be used to save space in structures having several binary flags or other small fields, and they can also be used in an attempt to conform to externally-imposed storage layouts. (Their success at the latter task is mitigated by the fact that bit-fields are assigned left-to-right on some machines and right-to-left on others).

Note that the colon notation for specifying the size of a field in bits is only valid in structures (and in unions); you cannot use this mechanism to specify the size of arbitrary variables. (See questions 1.2 and 1.3.)

 
 
Re:结构/联合体中的冒号问题
[ 2007-9-13 8:56:04 | By: wangdongx40423p(游客) ]
 
 
个人主页 | 引用 | 返回 | 删除 | 回复

发表评论:

    大名:
    密码: (游客无须输入密码)
    主页:
    标题:

时 间 记 忆
最 新 评 论

专 题 分 类
最 新 日 志
最 新 留 言
搜 索
用 户 登 录
友 情 连 接
博 客 信 息


Powered by Oblog.