pointers-memory · beginner · ~15 min
Understand pointer-to-pointer to mutate caller variables.
Implement void swap_ptrs(int **a, int **b) that swaps the two int* pointers the caller holds.
Swapping two pointers via a function is the most direct demonstration that C is pass-by-value — to mutate a caller's pointer, you need a pointer to that pointer.
Two non-NULL int **.
The caller sees the pointers swapped.
No allocations. No data dereferenced — just swap the pointers themselves.
void swap_ptrs(int **a, int **b) { /* TODO */ }
Swapping *a and *b instead of a and b (changes the pointed-to values, not the pointers). Using a temporary of the wrong type (int t instead of int *t).
Both pointers alias (a == b) — swap is a no-op. Either pointer is NULL — swap still works, just swaps NULLs around.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.