算法笔记—2.7.5 指针的引用
/*
P69-指针的引用*/
#include <stdio.h>
void sawp(int* &p1,int* &p2);
int main(void)
{
int a = 1,b = 2;
//scanf("%d%d",&a,&b);
int* p1 = &a;
int* p2 = &b;
sawp(p1,p2);
//printf("a= %d,b = %d",a,b);
printf("a= %d,b = %d",*p1,*p2);
return 0;
}
void sawp(int* &p1,int* &p2)
{
int* temp = p1;
p1 = p2;
p2 = temp;
}
/*
printf("a= %d,b = %d",a,b);
输出结果:
a= 1,b = 2
------------------------
printf("a= %d,b = %d",*p1,*p2);
输出结果:
a= 2,b = 1
理解:
1.引用的本质就是对变量取了一个别名.
2.指针的引用,在本代码中.实际上是改变了指针的指向.也就是p1,p2所存放的地址互换了.
*/