typestar

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

  1. A struct Counter * refers to a caller's struct.
  2. c->count dereferences and selects a field.
  3. A const pointer promises not to modify it.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Structs, step 20 of 25 in Pointers & memory.

← Previous Next →