Swap with Pointers
Completed
Write void swap(int *a, int *b). Read two numbers, swap them with your function and print them.
Input
Two integers.
Output
One line.
Example
Input
3 9
Output
9 3
Editor
C
Hints
C passes copies of arguments; passing addresses lets the function change the caller's variables.
Solution explanation
A swap(int a, int b) would only swap its own copies - pointers are needed.
Show solution
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int main() {
int x, y;
scanf("%d %d", &x, &y);
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}