ALEXKK2011

The Technical Side of alexKK2011
  博客园  :: 新随笔  :: 订阅 订阅  :: 管理

difference between pointer and reference

Posted on 2011-02-24 22:18  alexkk2011  阅读(164)  评论(0编辑  收藏  举报
#include <iostream>

using namespace std;

void swapf(char* a, char* b)
{
    int n1 = *a, n2 = *b;
    n1=n1+n2;
    n2=n1-n2;
    n1=n1-n2;
    *a=n1,    *b=n2;
};

void main()
{
    char x ='a', y='B';
    char *px = &x, *py = &y;
    char &rx = x, &ry =y;
    swapf(&x, &y);
    cout << x << " " << y << endl;
    swapf(&rx, &ry);
    cout << x << " " << y << endl;
    swapf(px, py);
    cout << x << " " << y << endl;
}
3-1