指针 *
引用 &
如下代码显示两者不同:
#include "stdafx.h"
#include<iostream>
using namespace std;
void swapr(int & a, int & b);
void swapp(int * p, int * q);
void swapv(int a, int b);
int _tmain(int argc, _TCHAR* argv[])
{
int wallet1 = 300;
int wallet2 = 350;
cout << wallet1 << endl;
cout << wallet2 << endl;
cout << endl;
swapr(wallet1, wallet2);
cout << wallet1 << endl;
cout << wallet2 << endl;
cout << endl;
swapp(& wallet1, & wallet2);
cout << wallet1 << endl;
cout << wallet2 << endl;
cout << endl;
swapv(wallet1, wallet2);
cout << wallet1 << endl;
cout << wallet2 << endl;
return 0;
}
void swapr(int & a, int & b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void swapp(int * p, int * q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
void swapv(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}