首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

结构体中的冒号代表什么解决方案

2012-02-14 
结构体中的冒号代表什么在windowsAPI中定义的_DCB结构体中的冒号如DWORDfDtrControl:2和它后面的数值2是

结构体中的冒号代表什么
在windows   API中定义的_DCB结构体中的冒号如
DWORD   fDtrControl     :2;  
和它后面的数值2是什么意思?

原定义如下:


typedef   struct   _DCB   {    
DWORD   DCBlength;  
  DWORD   BaudRate;    
DWORD   fBinary     :1;  
  DWORD   fParity     :1;  
  DWORD   fOutxCtsFlow     :1;
    DWORD   fOutxDsrFlow     :1;
    DWORD   fDtrControl     :2;
    DWORD   fDsrSensitivity     :1;
    DWORD   fTXContinueOnXoff     :1;
    DWORD   fOutX     :1;  
  DWORD   fInX     :1;  
  DWORD   fErrorChar     :1;  
  DWORD   fNull     :1;  
  DWORD   fRtsControl     :2;  
  DWORD   fAbortOnError     :1;  
  DWORD   fDummy2     :17;    
WORD   wReserved;  
  WORD   XonLim;  
  WORD   XoffLim;    
BYTE   ByteSize;  
  BYTE   Parity;    
BYTE   StopBits;    
char   XonChar;  
  char   XoffChar;    
char   ErrorChar;  
  char   EofChar;  
  char   EvtChar;  
  WORD   wReserved1;
}   DCB,     *LPDCB;


[解决办法]
C Bit Fields

Syntax:
struct-declarator:
declarator

type-specifier declarator opt : constant-expression

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.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.

Bit fields are allocated within an integer from least-significant to most-significant bit. In the following code

Copy Code
struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
} test;

int main( void );
{
test.a = 2;
test.b = 31;
test.c = 0;
}


the bits would be arranged as follows:

Copy Code
00000001 11110010
cccccccb bbbbaaaa




[解决办法]
常量表達式的寬度
[解决办法]
你BAIDU一下位域
[解决办法]
位域 或者 位段

属于C中的一种基本语法
[解决办法]
这是占位的用这种方法可以使占的位数最少,例如
<PRE> struct date_struct {
BYTE day : 5, //
month : 4, //
year : 14; //
} date;则
|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|
| | | |
+------ year ---------------+ month +-- day --+


这样的话就可以控制该结构所占的字节数,只占用了23位,不到三个字节
操作的话可以这样
date.day = 12;

dateptr = &date;
dateptr-> year = 1852;

热点排行