C的引用:
#include <stdio.h>
void ex(int* x,int* y)
{
int* ptr=x;
x=y;
y=ptr;
printf("*x=%d,*y=%d\n",*x,*y);
}
int main(void)
{
int x=5;
int y=10;
ex(&x,&y);
return 0;
}
上述代码运行结果:*x=10,*y=5;
C++的引用
#include <stdio.h>
void ex(int &x,int &y)
{
int tmp=x;
x=y;
y=tmp;
printf("x=%d,y=%d\n",x,y);
}
int main(void)
{
int a=3;
int b=4;
ex(a,b);
printf("a=%d,b=%d\n",a,b);
return 0;
}
运行结果:
x=4,y=3;
a=4,b=3;