【发布时间】:2014-11-10 10:51:00
【问题描述】:
我有这个代码 - http://ideone.com/sXhWxf
#include <stdio.h>
int main(void) {
struct bitfield{
unsigned a:5;
unsigned c:5;
unsigned b:6;
} bit = {1,3,3};
char *p = (char*)&bit;
printf("%d\n",*p);
p++;
printf("%d\n",*p);
// I assumed that the bits are laid out in the below order in the memory.
// Spaces are just for clarity
// 00001 00011 000011
// Also, I asumed that the 'char' will take 8 bits. But I can't understand output.
// According to me the output should be - 8 195 (considering the 1st 8 bits &
// last eight bits for the printf statements)
return 0;
}
输出是 -
97
12
谁能帮我详细理解这个输出? (请阅读代码中的cmets)
另外,我在Wikipedia 上看到了这个声明,它说“位域的成员没有地址,因此不能与地址-of (&) 一元运算符一起使用。sizeof 运算符可能不适用于位字段。" 但我可以访问 'bit' 变量的地址。那个怎么样?我没有正确解释该声明吗?请指导我。
【问题讨论】:
标签: c++ c memory output bit-fields