#include <iostream>
using namespace std;
// Define swapPtr(int* a, int* b)
void swap_ptr(int *a, int *b){
int i = 0;
int *temp = &i; //set a temp pointer to store temp for swapping
*temp = *a; //store the value pointing at a into temp address
*a = *b; //store value pointing at b into a
*b = *temp; //store value that we previously stored into temp back into b
}
// Define swapRef(int& a, int& b)
void swap_ref(int &c, int &d){
int temp = c;
c = d;
d = temp;
}
int main() {
int x, y;
std::cin >> x >> y;
int a = x, b = y;
swap_ptr(&a, &b);
std::cout << "After swapPtr: a=" << a << " b=" << b << "\n";
int c = x, d = y;
swap_ref(c, d);
std::cout << "After swapRef: a=" << c << " b=" << d;
return 0;
}
Input
5 10
Expected Output
After swapPtr: a=10 b=5 After swapRef: a=10 b=5