可能重复:
实际使用零长度位域
为什么有些结构具有零宽度位域,为什么需要它?
struct foo {
int a:3;
int b:2;
int :0; // Force alignment to next boundary.
int c:4;
int d:3;
};
int main()
{
int i = 0xFFFF;
struct foo *f = (struct foo *)&i;
printf("a=%d\nb=%d\nc=%d\nd=%d\n", f->a, f->b, f->c, f->d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上述程序的输出是
manav@os-team:~/programs/test$ ./a.out
a=-1
b=-1
c=-8
d=0
Run Code Online (Sandbox Code Playgroud)
请解释为什么这些值是负数,以及结构内部这些变量的内存布局?