Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

":" (colon) in C struct - what does it mean? [duplicate]

Tags:

c++

c

People also ask

What does colon mean in struct?

Those are bit fields. Basically, the number after the colon describes how many bits that field uses.

What Does a colon mean in C?

It's commonly used to pack lots of values into an integral type. In your particular case, it defining the structure of a 32-bit microcode instruction for a (possibly) hypothetical CPU (if you add up all the bit-field lengths, they sum to 32).

What is a bit field in C?

Bit Fields in C Language In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner.

Why does C have structs?

A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the ...


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


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
};

It defines bit-fields of width 1 and 8.