Hi All I have written two codes
1.
#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int a = 10, b = 20;
cout << "value of a before swap " << a << endl;
cout << "value of b before swap " << b << endl;
swap(&a, &b);
cout << "value of a after swap " << a << endl;
cout << "value of b after swap " << b << endl;
cin.get();
}
2.
#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int a = 10, b = 20;
cout << "value of a before swap " << a << endl;
cout << "value of b before swap " << b << endl;
swap(a, b);
cout << "value of a after swap " << a << endl;
cout << "value of b after swap " << b << endl;
cin.get();
}
In both cases I am getting same output as value of a before swap 10 value of b before swap 20 value of a after swap 20 value of b after swap 10
My First question is Does swap(&a,&b) and swap(a,b) makes no difference to swap function??
But when i give same arguments to given below swap function
void swap(int &x, int &y)
{
int t;
t = x;
x = y;
y = t;
}
swap(a,b) gives no issue and work fine but when i pass value as swap(&a,&b) code gives Error error C2665: 'swap': none of the 3 overloads could convert all the argument types Why??