Simvik

Swap two values

Exchange two integers through pointers. An easy embedded C problem (Pointers), run and graded in the browser.

easy · Pointers · out-parameters

C passes arguments by value, so a function can change its caller's variables only through pointers.

Exchange the values that a and b point to. The two pointers may refer to the same variable.

Starter code

void swap_i32(int32_t *a, int32_t *b) {
  // Exchange *a and *b.
}

Example tests

Two different variables

int32_t a = 1, b = 2;
swap_i32(&a, &b);
printf("%d %d\n", a, b);

Expected output:

2 1

Negative values

int32_t a = -40, b = 1000;
swap_i32(&a, &b);
printf("%d %d\n", a, b);

Expected output:

1000 -40

Learn it first

  • Pointers · Addresses, arithmetic, const, void pointers and object lifetime.

More Pointers problems