An array bundles many values of the same type. A struct bundles values of different types under one name. That is how you model a real thing — a point with an x and a y, a record with a name and an age, a node with some data and a link to the next node. Structs are the building block behind every data structure in the challenges, so this lesson ties the whole section together.
A struct definition lists the fields it groups. Each field has its own type and name:
1struct Point
2{
3 int x;
4 int y;
5};That creates a new type, struct Point. You declare a variable of it and reach each field with a dot:
1struct Point p;
2p.x = 3;
3p.y = 4;
4printfThe fields live together in memory, in the order you declared them — a struct Point is just an int followed by another int.
Writing struct Point every time gets tiresome. A typedef gives the type a shorter name so you can drop the struct keyword, exactly as we did in the ASCII challenges introduction:
1typedef struct
2{
3 int x;
4 int y;
5} Point;There is one case where you must still name the struct even with a typedef — when a struct needs to refer to its own type — and that case is coming up in a moment.
Passing a whole struct to a function copies it, field by field. For a small struct that is fine, but it is wasteful for large ones, and — just like any argument passed by value — the function only changes its own copy, not your original. So in practice you pass a pointer to the struct instead, the same move you learned in Pointers as function arguments.
When you have a pointer to a struct, reaching a field through it needs the arrow operator, ->:
1void move_right(Point *p)
2{
3 p->x = p->x + 1; // p->x is shorthand for (*p).x
p->x means "dereference p, then take its x." It is pure shorthand for (*p).x, but you will almost always write the arrow — it is everywhere in C once pointers and structs meet.
Here is the case that needs a named struct: a struct with a pointer to its own type. This is how you build linked structures — lists, trees, and the like — where each piece points to the next:
1typedef struct Node
2{
3 int data;
4 struct Node *next; // a pointer to another NodeThe field next holds the address of another Node. Chain a few together and each one points to the following one, with the last pointing at NULL to mark the end:
We can build exactly that and follow the chain in the debugger, hopping from node to node through next:
1#include <stdio.h>
2#include <stdlib.h>
3
4typedef struct Node
gcc -g list.c -o listset output-radix 10set print pretty offbreak 20Breakpoint 1 at 0x11c9: file list.c, line 20.runBreakpoint 1, main () at list.c:20p *head$1 = {data = 10, next = 0x5555555592c0}p head->data$2 = 10p head->next->data$3 = 20p head->next->next->dataEach head->next walks one link down the chain — 10, then 20, then 30 — until next is NULL (0x0), which is how you know you have reached the end. This is the backbone of the serialized challenges like Linked Ops and Tree.
Finally, a detail that matters the moment you read structs from a file. A struct's fields are stored in order, but the compiler is allowed to insert unused padding bytes between them so each field starts at an address its type likes to be aligned to. Take a struct with a char and an int:
1struct Mixed
2{
3 char c;
4 int x;
5};You might expect that to be 5 bytes — one plus four. It is almost always 8. The int wants to start on a 4-byte boundary, so the compiler leaves a 3-byte gap after the char:
The debugger confirms the size and the gap. The same program also defines a second, packed copy of the struct, which we come to right after:
1#include <stdio.h>
2
3struct Mixed
4{
5 char c;gcc -g pad.c -o padset output-radix 10set print pretty offbreak 20Breakpoint 1 at 0x1157: file pad.c, line 20.runBreakpoint 1, main () at pad.c:20p sizeof(m)$1 = 8p sizeof(pm)$2 = 5p (char *) &m.x - (char *) &m.c$3 = 4p (char *) &pm.x - (char *) &pm.csizeof(m) is 8, with the int sitting 4 bytes after the char — the padding in action. The packed PackedMixed is only 5 bytes, and its int sits just 1 byte after the char: no gap at all.
You get that tight layout with __attribute__((packed)), which tells the compiler to lay the fields out back to back with no padding:
1struct __attribute__((packed)) PackedMixed
2{
3 char c;
4 int x;So why is padding, not packing, the default? Because alignment is about speed. A CPU reads an aligned value — a 4-byte int on a 4-byte boundary — in a single fast access; an int that straddles a boundary takes extra work, and on some processor architectures it is slower still or even faults outright. The compiler pads by default so your data stays aligned and fast, and makes you opt in to packed when you specifically need an exact byte layout.
That need is exactly the serialized challenges: a file or network format packs its fields with no padding, so a plain struct read straight off disk will not line up. You have two fixes — read the fields one at a time, or declare the struct packed so its layout matches the file. Either way, never assume sizeof(struct) equals the number of bytes on disk.
That completes the data-structures toolkit — arrays, strings, and structs are the shapes almost every C program pushes its data into. Next we start getting real data into them, beginning one level down with the file descriptors that every file and socket is built on.