Kodewick
Start learning →
Basic←→0 / 70

Swap seats

#051BasicPointersFunctions4 min

At the cinema Mia has seat 7 and Leo seat 12, and they want to swap. Finish swap(a, b) so the two numbers a and b point to trade places. The old *a is already saved in tmp.

Should show
Mia: 12, Leo: 7
main.c
#include <stdio.h>

void swap(int *a, int *b) {
    int tmp = *a;
    // your code here: put *b into *a, then tmp into *b
}

int main(void) {
    int mia = 7, leo = 12;
    swap(&mia, &leo);
    printf("Mia: %d, Leo: %d\n", mia, leo);
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.