Simvik

Pack a wire header

Declare a struct that matches a 7-byte protocol header exactly. A medium embedded C problem (Structs, unions and memory layout), run and graded in the browser.

medium · Structs, unions and memory layout · packed · protocols

A protocol frame starts with a 7-byte header, with no gaps between fields:

  • type: 1 byte
  • length: 2 bytes
  • sequence: 4 bytes

Complete frame_header_t so that it has exactly this layout. The members must keep these names and this order.

Starter code

typedef struct {
  uint8_t type;
  uint16_t length;
  uint32_t sequence;
} frame_header_t;

Example tests

Size is 7 bytes

printf("%u\n", (unsigned)sizeof(frame_header_t));

Expected output:

7

Field offsets

printf("%u %u %u\n", (unsigned)offsetof(frame_header_t, type), (unsigned)offsetof(frame_header_t, length), (unsigned)offsetof(frame_header_t, sequence));

Expected output:

0 1 3

Learn it first

More Structs, unions and memory layout problems