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
  • 16,879
  • 27
  • 109
  • 172

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
  • 380,411
  • 33
  • 383
  • 585
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
  • 8,727
  • 6
  • 38
  • 61
1

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

akaHuman
  • 1,292
  • 1
  • 13
  • 33