Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 13 / 21 · PROGRAM STRUCTURE

Structures and records.

Model related fields, copying, member access, and arrays of records.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

A struct groups named members into one object. It lets a program say that an identifier and a quantity belong to the same record instead of passing parallel arrays whose positions must stay aligned. A struct type defines member names and types; a variable declaration creates an object of that type. The compiler lays out members with any padding needed by the implementation, so the in-memory bytes are not automatically a portable file format.

A struct can be assigned to another object of the same type, copying its member values. If a member is a pointer, this copies the pointer value, not the pointed-to allocation. That is a shallow copy. A struct with fixed-size array members includes those elements in the assignment, but a struct with dynamically owned data needs a deliberate copy and cleanup policy.

02 / THE RULES

What C guarantees.

The dot operator accesses a member through a struct object. The arrow operator accesses a member through a pointer to a struct; p->member is equivalent to (*p).member. A function can receive a const struct * to avoid copying a large record while promising not to modify it through that pointer. Returning a small struct by value can be clear and efficient when it represents one result.

Arrays of structs keep records contiguous. That supports iteration, but a record's pointer members may still refer to storage elsewhere. Before serializing a record, write fields explicitly into a defined representation instead of dumping sizeof(struct) bytes. The example keeps only value members so its copy has an intuitive meaning.

03 / UNDER THE SURFACE

Why the detail matters.

Member order in a struct is preserved in the sense that later non-bit-field members have increasing addresses, but padding may appear between them and at the end. sizeof(struct Item) can therefore be larger than the sum of its member sizes. That padding serves alignment and implementation needs; it should not become an accidental external data format. Serialize named fields in a defined order with defined widths when another process or machine must read them.

Struct assignment copies member values as part of one language operation. It does not recursively duplicate allocations referenced by pointer members. If a struct owns a heap buffer, two shallow copies may both appear to own the same allocation, leading to double-free or shared mutation errors. A design can avoid this by keeping ownership in one place, providing an explicit deep-copy function, or storing only borrowed pointers with documented lifetimes. C does not enforce which convention you choose.

04 / CODE, EXPLAINED

See the rule in context.

structures-and-records.c
#include <stdio.h>

struct Item {
    int code;
    int quantity;
};
int main(void) {
    struct Item first = {17, 4};
    struct Item second = first;
    second.quantity += 2;
    printf("%d, %d\n", first.quantity, second.quantity);
    return 0;
}
FOLLOW THE EXECUTION

first contains code 17 and quantity 4. The assignment to second copies those two integer member values into another struct object. Updating second.quantity changes only second, so the displayed line is 4, 6. The objects do not share the integer fields. If a field were a pointer, both copies could still point to the same separate allocation.

05 / THE BOUNDARY

Where the rule stops.

Padding bytes and member alignment can vary across compilers and targets. Do not use raw struct memory as a stable network packet or disk record without an explicit format contract.

06 / THE CONNECTION

Keep this model.

A struct says several fields coexist. A union says several interpretations share one storage region, while an enum gives names to a set of integer choices. Those tools can model alternatives, but they need careful rules about which interpretation is active.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.