武汉网站建设公司排名随州今日头条新闻
一句话解释位域:指定结构体内变量的的位宽,从而节省空间
例子:
#include <stdio.h>
struct _test
{int bit1:3; // 第一个字节0 ~ 2位 int :0; // 空域:表示第一个字节 3~7都为0int bit2:1; // 第二个字节第0位int :3; // 第二个字节1~3位保留不可使用
}test,*ptest;int main(void) {ptest = &test; //结构体指针ptest->bit1 = 1; //bit1 取值范围为:0~7 因为只有三个位printf("bit1=%d\n",ptest->bit1);printf("test占用%d字节",sizeof(test));return 0;
}
运行结果:
bit1=1 test
占用8字节