指针做子函数参数
#include<stdio.h>
void swap1(short m,short n)
{
short temp=0;
temp=m;
m=n;
n=temp;
}
void swap2(short *pm,short *pn)
{
short *temp=NULL;
temp=pm;
pm=pn;
pn=temp;
}
void swap3(short *pm,short *pn)
{
short temp=0;
temp=*pm;
*pm=*pn;
*pn=temp;
}
main()
{
short x,y=0;
x=0x1234;
y=0x5678;
printf("before swap1() exchange:x=%x,y=%x\n",x,y);
swap1(x,y);//形参互换,未影响实参。
printf("after swap1() exchange:x=%x,y=%x\n",x,y);
printf("before swap2() exchange:x=%x,y=%x\n",x,y);
swap2(&x,&y);//实参为x,y地址,形参pm(指向x),pn(指向y)互换,未对其所指内存做读写操作
printf("after swap2() exchange:x=%x,y=%x\n",x,y);
printf("before swap3() exchange:x=%x,y=%x\n",x,y);
swap3(&x,&y);//实,形参与swap2类似,互换指针pm,pn所指内存区域的数据(x,y)
printf("after swap3() exchange:x=%x,y=%x\n",x,y);
return 0;
}
来源:CSDN
作者:丹青客
链接:https://blog.csdn.net/huangziguang/article/details/104736164