-4

I came across a syntax I've never seen before.

typedef struct SomeStruct
{
    unsigned char ip_version :4;
    unsigned char ip_tos;
    unsigned char ip_frag_offset :5;
    unsigned char ip_more_fragment :1;
    unsigned char ip_dont_fragment :1;
    unsigned char ip_reserved_zero :1;
    unsigned char ip_frag_offset1;
    unsigned char ip_ttl;
} SomeHeader;

What is the colon #; for? Is that the same as using the assignment operator? Why use the colon and is there advantages or a reason this is allowed?

Brandon
  • 22,723
  • 11
  • 93
  • 186

1 Answers1

1

These are bit fields:

It should be noted you could have found this by searching Google for "c struct syntax". For me, the first result was wikipedia, which has a bit field section

cegfault
  • 6,442
  • 3
  • 27
  • 49