":" (colon) in C struct - what does it mean?
C++CC++ Problem Overview
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?
C++ Solutions
Solution 1 - C++
Those are bit fields. Basically, the number after the colon describes how many bits that field uses. Here is a quote from MSDN describing bit fields:
> 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
Solution 2 - C++
I also ran into the colon notation but in my context bit fields didn't make sense. So I did some digging. This notation is also used for assigning values - in my specific situation pointers to functions.
Source: http://www.tldp.org/LDP/lkmpg/2.4/html/c577.htm
Below is a sample and an excerpt to explain.
"There is a gcc extension that makes assigning to this structure more convenient. You'll see it in modern drivers, and may catch you by surprise. This is what the new way of assigning to the structure looks like:"
struct file_operations fops = {
read: device_read,
write: device_write,
open: device_open,
release: device_release
};
The C99 (old, compatible) way looks like:
struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
Solution 3 - C++
It defines bit-fields of width 1 and 8.