指针和引用

来源:互联网 发布:faker知乎 编辑:程序博客网 时间:2024/05/16 11:34

指针 *
引用 &
如下代码显示两者不同:

#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;}
原创粉丝点击