#include
using namespace std;
//交换函数
//1.值传递
void myswap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//2.地址传递
void myswap02(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//3.引用传递
void myswap03(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10; int b = 20;
myswap01(a, b);//值传递,形参不会修饰实参
cout <<"a=" << a << endl;
cout <<"b=" << b << endl;
myswap02(&a, &b);//地址传递,形参会修饰实参
cout << "a=" << a << endl;
cout << "b=" << b << endl;
myswap03(a, b);//引用传递,形参会修饰实参
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
用的vs2022,难道是缺少插件吗?
第三个函数引用传递并没有交换a和b的值