代码改变世界

关于结构体中带:的赋值

2016-07-16 14:47  小竹心语  阅读(343)  评论(0编辑  收藏  举报
 1 #include <stdio.h>
 2 #include <string.h>
 3  //using namespace std;
 4   typedef struct
 5  {
 6 
 7     unsigned int T_Alert       ;
 8     unsigned int Reserve1      : 3;
 9     unsigned int ResetDetected : 1;
10     unsigned int Reserve0      : 2;
11     unsigned int CmdStatus     : 1;
12     unsigned int CrcStatus     : 1;
13         
14 } regStatus;
15 
16 
17   int main(int argc , char *argv[])
18  {
19     regStatus aaa;
20     aaa.Reserve1=7;//在范围之内可以编译通过
21         aaa.Reserve1=8;//超出范围会报错
22     printf("%d\n",aaa.T_Alert);
23     printf("%d\n",aaa.Reserve1);
24     
25      return 0;
26  }    

 

分析,比如说结构体中的这个变量

unsigned int Reserve1      : 3; 
意思是说这个变量占这个数据类型的三位 把这个值赋值0-7都是对的,赋值超过7都是错的