0

Possible Duplicate:
What does 'unsigned temp:3' means

A struct's definition goes like this,

typedef struct
{   
    uint32_t length : 8;  
    uint32_t offset : 24;
    uint32_t type : 8;
} A;

I haven't seen this kind of definition before, what does it mean by :8 and :24?

Community
  • 1
  • 1
Alcott
  • 17,905
  • 32
  • 116
  • 173

3 Answers3

6

It's defining bitfields. This tells the compiler that length is 8 bits, offset is 24 bits and type is also 8 bits.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

Refer the following link. They are bit-fields. http://www.cs.cf.ac.uk/Dave/C/node13.html
http://en.wikipedia.org/wiki/Bit_field

#include <stdio.h>

typedef unsigned int uint32_t;

#pragma pack(push, 1)
typedef struct
{
    uint32_t length : 8;
    uint32_t offset : 24;
    uint32_t type : 8;
} A;

typedef struct
{
    uint32_t length;
    uint32_t offset;
    uint32_t type;
} B;
#pragma pack(pop)

int main()
{
  printf("\n Size of Struct: A:%d   B:%d", sizeof(A), sizeof(B));
  return 0;
}

Structure A's size will be 5 Bytes where as B's size will be 12 bytes.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
1

This notation defines bit fields, i.e. the size in number of bits for that structure variable.

akaHuman
  • 1,332
  • 1
  • 14
  • 33