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 */
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.