#include <iostream>
using namespace std;
void fun(int **x, int **y);
int main()
{
int a = 5;
int b = 10;
int *x = &a, *y = &b;
cout << *x << "," << *y << endl;
cout << x << "," << y << endl;
fun(&x, &y);
cout << *x << "," << *y << endl;
cout << x << "," << y << endl;
return 0;
}
void fun(int **x, int **y) //要交换指针地址,要用二级指针(指针就是一个地址,故传入&x,&y)...
{
int *p = NULL;
p = *y;
*y = *x;
*x = p;
}
/*void fun(int *x, int *y)//函数调用的时候,形参是实参的副本。所以这里交换的指针地址只是实参的副本,原来的指针值并没有改变。
{
int *p = NULL;
p = y;
y = x;
x = p;
}
*/