您的位置:首页 > 编程语言 > C语言/C++

C++引用和指针简单样例对比

2016-10-25 10:30 477 查看
宏swap  http://blog.csdn.net/u014646950/article/details/51603374

c中没有引用,c++才有引用

所以此处用的g++编译

//compile:g++ p_quote.c
//run: ./a.out
//c中没有引用,C++中有引用

#include<stdio.h>
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
void pq(int &a,int &b)
{

printf("quote a=%d b=%d\n",a,b);
printf("quote &a=%d &b%d\n",&a,&b);
swap(a,b);//交换

}
void p(int *a,int*b)
{
printf("point a=%d b=%d\n",a,b);
printf("point &a=%d &b=%d\n",&a,&b);
printf("point *a=%d *b=%d\n",*a,*b);
swap(*a,*b);//交换
}

int main()
{

int aa=0,bb=1;
printf("main val %d %d\n",aa,bb);
printf("main addr %d %d\n",&aa,&bb);
pq(aa,bb);
printf("after quote  swap:a=%d b=%d\n",aa,bb);
p(&aa,&bb);
printf("after point  swap:a=%d b=%d\n",aa,bb);
return 0;
}


屏幕输出

main val 0 1
main addr -1451064500 -1451064504
quote a=0 b=1
quote &a=-1451064500 &b-1451064504
after quote  swap:a=1 b=0
point a=-1451064500 b=-1451064504
point &a=-1451064536 &b=-1451064544
point *a=1 *b=0
after point  swap:a=0 b=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux