":" (冒号)在C结构 - 这是什么意思? [英] ":" (colon) in C struct - what does it mean?

查看:136
本文介绍了":" (冒号)在C结构 - 这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 结构_USBCHECK_FLAGS
    {
        unsigned char型DEVICE_DEFAULT_STATE:1;
        unsigned char型DEVICE_ADDRESS_STATE:1;
        unsigned char型DEVICE_CONFIGURATION_STATE:1;
        unsigned char型DEVICE_INTERFACE_STATE:1;
        unsigned char型FOUR_RESERVED_BITS:8;
        unsigned char型RESET_BITS:8;
    } State_bits;

这是什么 1 :8 的意思是


解决方案

这些都是位域。一些网络搜索将产生更多关于他们的信息比你所能想:)但基本上冒号后的数字说明有多少位该领域的使用。

编辑,包括信息从MSDN 以prevent破链接问题:


  

的恒定前pression指定字段的位宽度。该
  类型说明符的声明符必须无符号整数,符号整数,或者
  int和恒定前pression必须是一个非负整数值。
  如果值是零,声明没有声明。位阵列
  领域,指针位字段,并返回位字段功能
  不允许。可选的说明符名称位域。位域
  只能被声明为结构的一部分。该地址运算符
  (安培)。不能应用于位字段组分


  
  

未命名位字段不能被引用,并在运行其内容
  时间是联合国predictable。它们可以被用作虚拟的字段,用于
  对齐的目的。一名不愿透露姓名位域的宽度被指定为0
  保证存储的构件在下面的它
  结构声明列表开始一个int边界上。


  
  

这个例子定义了一个名为屏结构的二维数组。


 结构
{
    无符号短图标:8;
    无符号短颜色:4;
    无符号短下划线:1;
    无符号短闪烁:1;
}屏幕[25] [80];

Edit:从MSDN链接另一个重要的一点:


  

位字段具有相同的语义整数类型。这意味着
  比特字段中的前pressions应用于完全相同的方式作为变量
  相同的基本类型的将被用于不管有多少位,
  在位字段。


有一个快速的样本说明了这一点很好。有趣的是,混合型编译器似乎默认为的sizeof(INT)

 结构
  {
    int类型的:4;
    INT A:13;
    INT C:1;
  } TEST1;  结构
  {
    短:4;
    短期A:3;
  } TEST2;  结构
  {
    所以char a:4;
    CHAR乙:3;
  } TEST3;  结构
  {
    所以char a:4;
    短期A:3;
  } TEST4;  的printf(测试1:%d个\\ ntest2数:%d \\ ntest3数:%d \\ ntest4数:%d \\ n,sizeof的(TEST1)的sizeof(TEST2)的sizeof(TEST3)的sizeof(TEST4));


  

测试1:4


  
  

测试2:2


  
  

TEST3:1


  
  

TEST4:4


struct _USBCHECK_FLAGS
    {
        unsigned char   DEVICE_DEFAULT_STATE       : 1;
        unsigned char   DEVICE_ADDRESS_STATE       : 1;
        unsigned char   DEVICE_CONFIGURATION_STATE : 1;
        unsigned char   DEVICE_INTERFACE_STATE     : 1;
        unsigned char   FOUR_RESERVED_BITS         : 8;
        unsigned char   RESET_BITS                 : 8;
    } State_bits;

What does :1 and :8 mean?

解决方案

Those are bitfields. Some web searching will yield more info about them than you could ever want :) But basically the number after the colon describes how many bits that field uses.

Edit to include info from MSDN to prevent broken link problems:

The constant-expression specifies the width of the field in bits. The type-specifier for the declarator must be unsigned int, signed int, or int, and the constant-expression must be a nonnegative integer value. If the value is zero, the declaration has no declarator. Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The address-of operator (&) cannot be applied to bit-field components.

Unnamed bit fields cannot be referenced, and their contents at run time are unpredictable. They can be used as "dummy" fields, for alignment purposes. An unnamed bit field whose width is specified as 0 guarantees that storage for the member following it in the struct-declaration-list begins on an int boundary.

This example defines a two-dimensional array of structures named screen.

struct 
{
    unsigned short icon : 8;
    unsigned short color : 4;
    unsigned short underline : 1;
    unsigned short blink : 1;
} screen[25][80];

Edit: another important bit from the MSDN link:

Bit fields have the same semantics as the integer type. This means a bit field is used in expressions in exactly the same way as a variable of the same base type would be used, regardless of how many bits are in the bit field.

A quick sample illustrates this nicely. Interestingly, with mixed types the compiler seems to default to sizeof (int).

  struct
  {
    int a : 4;
    int b : 13;
    int c : 1;
  } test1;

  struct
  {
    short a : 4;
    short b : 3;
  } test2;

  struct
  {
    char a : 4;
    char b : 3;
  } test3;

  struct
  {
    char a : 4;
    short b : 3;
  } test4;

  printf("test1: %d\ntest2: %d\ntest3: %d\ntest4: %d\n", sizeof(test1), sizeof(test2), sizeof(test3), sizeof(test4));

test1: 4

test2: 2

test3: 1

test4: 4

这篇关于":" (冒号)在C结构 - 这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆