":" (colon) in C struct - what does this mean?

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 do :1 and :8 mean?

+51
c ++ c
Dec 19 '11 at 16:45
source share
3 answers

These are bit fields. Basically, the number after the colon describes the number of bits that the field uses. The following is a quote from MSDN that describes bit fields:

A constant expression indicates the width of the field in bits. The qualifier type for the declarator must be unsigned int, signed by int or int, and the constant expression must be a non-negative integer value. If the value is zero, the declaration does not have a declarator. Arrays of bit fields, pointers to bit fields, and functions that return bit fields are not allowed. The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The operator address (&) cannot be applied to bit field components.

Invalid bit fields cannot be specified, and their contents are unpredictable at startup. They can be used as dummy fields for alignment. An unnamed bit field whose width is set to 0 ensures that the storage for the member following it in the struct-declaration-list starts with an int border.

This example defines a two-dimensional array of structures called 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 that the bit is used in expressions in exactly the same way as a variable of the same basic type, regardless of how many bits are in the bit field.

A quick sample illustrates this well. Interestingly, with mixed types, the compiler defaults 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

+42
Dec 19 '11 at 16:49
source share

It defines bit fields 1 and 8 wide.

+6
Dec 19 '11 at 16:46
source share

I also got into colon notation, but in my context, bit fields didn't make sense. So I did the digging. This notation is also used to assign values ​​- in my particular situation, function pointers.

Source: http://www.tldp.org/LDP/lkmpg/2.4/html/c577.htm

Below is a sample and excerpt for explanation.

" There is a gcc extension that makes this structure more convenient . You will see it in modern drivers and you may be surprised. This new way of assigning a structure looks like this:"

 struct file_operations fops = { read: device_read, write: device_write, open: device_open, release: device_release }; 

The C99 (old, compatible) way looks like this:

 struct file_operations fops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release }; 
+4
Oct 30 '13 at 18:32
source share



All Articles