Structs
A struct is a named collection of members that sit together in memory. You access members with . on a struct value and -> on a pointer to one.
Detailed Explanation
struct Point { int x; int y; }; defines a type. It does not allocate. struct Point p; allocates one object. Members are laid out in declaration order, each aligned to its own requirement, with padding between them and possibly at the end so that an array of the struct keeps every element aligned. sizeof(struct Point) can be larger than sizeof(int) + sizeof(int). Do not send a struct over the network by dumping its bytes unless you have frozen the layout, the endianness, and the padding.
C passes structs by value. void move(struct Point p) receives a copy. void move(struct Point *p) receives an address. For any struct larger than a couple of registers, pass a pointer. For a read-only view, pass const struct Point *. Returning a struct by value is legal and common for small records.
Initialization uses braces. struct Point p = { 3, 4 }; fills in order. struct Point p = { .y = 4, .x = 3 }; names members and leaves the rest zero. A nested struct or array member is initialized with nested braces or a designated path .corner.x = 1. Assignment copies every byte, including padding bits; those bits may be uninitialized if a member was set by field assignment rather than a full initializer, which is why memcmp of two structs is a fragile equality test. Compare members.
A struct can contain other structs, arrays, and pointers. It cannot contain an instance of itself — that would be infinite size — but it can contain a pointer to itself, which is how linked lists work. A flexible array member may appear as the last member and is a separate topic.
Anonymous structs and unions, and typedef struct Point Point;, are style. The typedef saves you from writing struct at every use and hides the tag. A public header that wants to keep the layout secret does not define the members at all; it uses an opaque type.
Code Examples
#include <stdio.h>
struct Point {
int x;
int y;
};
static struct Point add(struct Point a, struct Point b) /* pass and return by value */
{
return (struct Point){ .x = a.x + b.x, .y = a.y + b.y };
}
static void offset(struct Point *p, int dx, int dy) /* mutate through a pointer */
{
p->x += dx;
p->y += dy;
}
int main(void)
{
struct Point p = { .x = 1, .y = 2 };
p = add(p, (struct Point){ 3, 4 });
offset(&p, 1, 0);
printf("%d %d\n", p.x, p.y); /* prints: 5 6 */
return 0;
}#include <stdio.h>
#include <stddef.h>
struct Rec {
char flag;
int id; /* padding before id on most ABIs */
};
int main(void)
{
printf("sizeof=%zu flag@%zu id@%zu\n",
sizeof(struct Rec),
offsetof(struct Rec, flag),
offsetof(struct Rec, id));
/* typical 64-bit: sizeof=8 flag@0 id@4 */
return 0;
}Common Gotchas
Padding is real
a struct of a `char` and an `int` is often 8 bytes, not 5. Reorder members by descending alignment if you are packing a table of millions.
`memcmp` on structs can disagree with member-wise equality
padding bytes may differ. Compare the fields you mean.
`p.x` versus `p->x`
the first is a value (or an lvalue of struct type). The second is a pointer. There is no third operator.
Copying a struct that owns a pointer copies the pointer, not the pointee
two structs then share one allocation. Decide whether the type is a value or a handle.