​​宏swap​​

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