Struct pointers in C
Mutating a struct through a pointer with ->.
struct Counter {
int count;
int step;
};
void advance(struct Counter *c) {
c->count += c->step;
}
int current(const struct Counter *c) {
return c->count;
}
How it works
- A
struct Counter *refers to a caller's struct. c->countdereferences and selects a field.- A
constpointer promises not to modify it.
Keywords and builtins used here
Counteradvanceconstcurrentintreturnstructvoid
The run, in numbers
- Lines
- 12
- Characters to type
- 157
- Tokens
- 49
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 31 seconds.
Step 2 of 3 in Structs, step 20 of 25 in Pointers & memory.