I have tested the code below
#include <iostream>
using namespace std;
void swap(int *x, int *y);
int main() {
int a, b;
a = 5;
b = 10;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "&a = " << &a << endl;
cout << "&b = " << &b << endl;
swap(a, b);
cout << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
void swap(int *x, int *y){
cout << "Hello" << endl;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
int temp;
temp = *x;
*x = *y;
*y =temp;
}
I know it should pass &a and &b to swap and that works as expected. However, above codes seem work as well. Results are:
a = 5
b = 10
&a = 0x7ffeea1648d8
&b = 0x7ffeea1648d4
a = 10
b = 5
Questions are:
If the swap function is implemented why there is no info printed out?
If swap function is not implemented why the values swap?