&是取地址运算符,表明只把变量的地址传出去,改变变量的值。
*是传递指针,虽然也改变变量的值,但如果要改变原变量的地址时,则不会影响主调函数中该变量的地址,而&则会改变。
你可以用一个实例去做个实验。
int main()
{
int a = 1 , b = 2;
swap1(a, b);
cout <<"a = "<< a << " ,"<<"b="<<b;
swap2(a,b);
cout <<"a = "<< a << " ,"<<"b="<<b;
swap3(a,b);
cout <<"a = "<< a << " ,"<<"b="<<b;
return 0;
}
swap1(int a, int b)
{
int t;
t = a;
a = b;
b = t;
}
swap2(int &a, int &b)
{
int t;
t = a;
a = b;
b = t;
}
swap2(int *a, int *b)
{
int *t; //注意
t = a;
a = b;
b = t;
}