我不知道为什么,但位字段分配没有按预期工作。可能只是一件愚蠢的事情,但我无法找到问题所在。
任何帮助都非常受欢迎。
typedef struct a {
unsigned char a1 :1;
unsigned char a2 :3;
unsigned char a3 :2;
unsigned char a4 :2;
} __attribute__((packed)) mystruct;
int main() {
mystruct d;
d.a1 = 0;
d.a2 = 2;
d.a3 = 1;
d.a4 = 2;
unsigned char *val = (unsigned char*) &d;
printf("%02X \n", *val);
printf("%02X \n", sizeof(hola));
exit(0);
}
返回输出:
94
01
预期输出:
26
01