C++中函数传递参数的三种方式
1、值传递:只在函数内起作用
2、地址传递:需注意解地址
3、引用传递:给变量起别名,实质上还是地址引用
#include<iostream>
#include<string>
using namespace std;
void remove1(int a,int b) {
int temp;
temp = a;
a = b;
b = temp;
cout << "remove1中a的值为" << a << endl;
cout << "remove1中b的值为" << b << endl;
}
void remove2(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
cout << "remove1中a的值为" << *a << endl;
cout << "remove1中b的值为" << *b << endl;
}
void remove3(int &a, int& b) {
int temp;
temp = a;
a = b;
b = temp;
cout << "remove1中a的值为" << a << endl;
cout << "remove1中b的值为" << b << endl;
}
int main() {
int a = 10;
int b = 20;
//remove1(a, b);
//remove2(&a, &b);
remove3(a, b);
cout << "a的值为" << a << endl;
cout << "b的值为" << b << endl;
return 0;
}