#include<iostream>
using namespace std;
void jh1(int x, int y) //值传递
{
int temp = x;
x = y;
y = temp;
cout << "交换后,jh1函数中 a=" << x << endl;
cout << "交换后,jh1函数中 b=" << y << endl;
}
void jh2(int* x, int* y) //地址传递
{
int temp = *x;
*x = *y;
*y = temp;
cout << "交换后,jh2函数中 a=" << *x << endl;
cout << "交换后,jh2函数中 b=" << *y << endl;
}
void jh3(int& x, int& y)//引用
{
int temp = x;
x = y;
y = temp;
cout << "交换后,jh3函数中 a=" << x << endl;
cout << "交换后,jh3函数中 b=" << y << endl;
}
int main()
{
int a = 10;
int b = 20;
jh1(a, b);
cout << "交换后,main函数中 a=" << a << endl;
cout << "交换后,main函数中 b=" << b << endl;
cout << endl;
jh2(&a, &b);
cout << "交换后,main函数中 a=" << a << endl;
cout << "交换后,main函数中 b=" << b << endl;
cout << endl;
a = 10;//由于jh2函数地址传递后a和b的值改变了,这里重新赋值,使a=10,b=20
b = 20;
jh3(a, b);
cout << "交换后,main函数中 a=" << a << endl;
cout << "交换后,main函数中 b=" << b << endl;
}
三种参数传值方式
最新推荐文章于 2024-05-16 01:53:10 发布