//HW_CH02_87
#include<iostream>
using namespace std;
void swap(float* px, float* py) {
float temp;
temp =* px;
*px = *py;
*py = temp;
}
int main() {
float x = 5.5876;
float y = 326.5;
cout << "x:" << x << " " << "y:" << y << endl;
swap(x, y);
cout << "after exchange:" << endl;
cout << "x:" << x << " " << "y:" << y << endl;
return 0;
}
2023/3/31
老师上课讲解
//HW_CH02_87
#include<iostream>
using namespace std;
void swap(float* px, float* py) {
float temp;
temp =* px;
*px = *py;
*py = temp;
}
int main() {
float x = 5.5876;
float y = 326.5;
cout << "x:" << x << " " << "y:" << y << endl;
swap(x, y);
cout << "after exchange:" << endl;
cout << "x:" << x << " " << "y:" << y << endl;
swap(&x,&y);
cout << "after exchange:" << endl;
cout << "x:" << x << " " << "y:" << y << endl;
return 0;
}
传参应当传变量的地址
但是为什么传变量也能实现交换?