basics · beginner · ~15 min
Why pointers exist: a function can mutate caller-owned values.
Exchange the values of two integers through pointers — your first pass-by-pointer function.
Implement void swap(int *a, int *b) that swaps the integers *a and *b. No main — the grader calls it.
Two non-NULL pointers a and b, each pointing to an int.
Nothing is returned. After the call, *a holds the old *b and vice versa.
int x=3, y=5; swap(&x, &y); -> x=5, y=3
swap(&g, &g) (same address twice): the value must stay unchanged.Two non-NULL int pointers a and b.
Nothing returned; *a and *b are exchanged.
Use a temporary; self-swap (same address) must leave the value unchanged.
void swap(int *a, int *b) {
/* TODO */
}
Swapping pointer variables locally (a = b) without dereferencing (*a and *b); forgetting temporary variable storage.
Both pointers pointing to the exact same address (&x, &x); negative values; INT_MIN and INT_MAX.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.