Structures and Unions
struct groups related fields; union lets several fields share the same memory.
Easy
typedef struct {
uint8_t id;
float temperature;
uint32_t timestamp;
} Reading;
Reading r = { .id = 1, .temperature = 24.5f };
r.timestamp = 1000;
union Word { uint32_t value; uint8_t bytes[4]; }; // view the same 4 bytes two ways
A struct's size can be larger than the sum of its fields because of padding for alignment. Unions are used for protocol parsing and type punning (byte order matters!).
Sub-topics (1)
Bit Fields
Pack several small values into the bits of one integer inside a struct.
Intermediate♥ 0🔖 0💬 0
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.