C/C++ Arena

Step 3 of 6

Pointers to structs and ->

Passing a big struct copies all of it. Passing a pointer is cheaper and lets the function modify the original.

With a pointer, (*p).kills is so common that C has the shortcut p->kills.

void add_kill(struct Player *p) {
    p->kills++;
}

Your turn: write void record_round(struct Player *p, int kills, int died) that adds kills to p->kills and adds 1 to p->deaths if died is non-zero.

Previous: Structs in functions Next: Arrays of structs