#include <stdio.h>
struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
}test;
int main(void)
{
int i = 0;
test.a = 2;
test.b = 3;
test.c = 0;
i = *((short *)&test);
printf("%d",i);
return 0;
}
之前还不懂
i = *((short *)&test);
这条语句是怎么工作的;问了一个高手后(老罗),现在知道为啥了。
&test 是取test的地址,
(short *)&test是把test的地址转换为short的地址
*((short *)&test)取这个地址指向的值
所以的i的之就是0000000000110010 (刚好是short类型的2个字节的大小)= 50.