交换两个指针的指向若用函数调用来实现的话必须用到二级指针,因为参数传递是值传递,相当于只把两个指针副本的指向交换了一下,若不用函数的话
一级指针也可以
1、一级指针
#include <stdio.h>
int main(){
int a,b;
int
*p1=&a,*p2=&b;
printf("%p\t%p\n",p1,p2);
int
*tmp;
tmp=p1;
p1=p2;
p2=tmp;
printf("%p\t%p\n",p1,p2);
return 0;
}
2、二级指针
#include <stdio.h>
void swap(int**x,int**y){
int* temp;
temp = *x;
*x = *y;
*y =
temp;
}
int main(int argc, char* argv[])
{
int a = 8;
int b =
88;
int* p = &a;
int* q = &b;
int** pp = &p;
int** qq = &q;
printf("&a=%p\n",p);
printf("&b=%p\n",q);
swap(pp,qq);
printf("&a=%p\n",p);
printf("&b=%p\n",q);
return 0;
}