Kodewick
Start learning →
Basic←→0 / 50

Swap with pointers

#049Basic*a = *b5 min

Finish swap(a, b) so the two variables in main really swap. a and b are pointers that hold the addresses of x and y; *a is x itself. The program swaps two pairs and shows them: 3 8 becomes 8 3.

Should show
8 3
100 -1
main.c
#include <stdio.h>

void swap(int *a, int *b) {
    // your code here: use a temp variable t to swap *a and *b
}

int main(void) {
    int x = 3, y = 8;
    int p = -1, q = 100;
    swap(&x, &y);
    swap(&p, &q);
    printf("%d %d\n", x, y);
    printf("%d %d\n", p, q);
    return 0;
}
Ctrl + Enter runs too
Result

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