关于交换两个数的问题
程序代码:
void swapr(int &a, int &b); void swapp(int *p, int *q); void swapv(int a, int b); int main() { int wallet1 = 300; int wallet2 = 350; cout << "wallet1 = $" << wallet1; cout << " wallet2 = $" << wallet2 << endl; cout << "using references to swap contents: " << endl; swapr(wallet1, wallet2); cout << "wallet1 = $" << wallet1; cout << " wallet2 = $" << wallet2 << endl; cout << "using pointers to swap contents again: " << endl; swapp(&wallet1, &wallet2); cout << "wallet1 = $" << wallet1; cout << " wallet2 = $" << wallet2 << endl; cout << "trying use passing by value:" << endl; swapv(wallet1, wallet2); cout << "wallet1 = $" << wallet1; cout <<" wallet2 = $" << 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; }
为什么利用指针交换两个数没有成功?请大神帮忙