basics · beginner · ~15 min

Swap via pointers

Why pointers exist: a function can mutate caller-owned values.

Challenge

Implement void swap(int *a, int *b) that swaps the integers *a and *b. This is your first pass-by-pointer.

Starter code

void swap(int *a, int *b) {
    /* TODO */
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.