Challenges/Memory & Types/sizeof() on Primitive Types

sizeof() on Primitive Types

๐ŸŸข Warm-up

sizeof() on Primitive Types

Write a function `describe_sizes` that fills in a `SizeInfo` struct with the sizeof each primitive type.

typedef struct {

size_t size_char;

size_t size_short;

size_t size_int;

size_t size_long;

size_t size_ptr;

} SizeInfo;

This is the most fundamental thing in embedded C โ€” always knowing how big your data is.

Why it matters: On a 32-bit ARM Cortex-M, `int` is 4 bytes. On an 8-bit AVR, `int` is 2 bytes. Wrong assumptions here corrupt memory silently.

example
typedef struct {
    size_t size_char;
    size_t size_short;
    size_t size_int;
    size_t size_long;
    size_t size_ptr;
} SizeInfo;

Test Cases

1.
size_char must be 1
size_char == 1
2.
size_int must be >= size_short
size_int >= size_short
3.
size_ptr must be 4 or 8
size_ptr == 4 || size_ptr == 8
solution.c
All modules
1 of 6 ยท Memory & Types
Next