#include<stdio.h>
void swap(int x,int y);
void swap(int x,int y){
int temp;
printf("In swap 互换前x=%d,y=%d\n",x,y);
temp =x;
x=y;
y=temp;
printf("In swap 互换后x=%d,y=%d\n",x,y);
}
int main(){
int x=3,y=5;
swap(x,y);
printf("In main 互换后x=%d,y=%d\n",x,y);
return 0;
}
运行结果:
通过指针传地址在内存里面直接修改参数的值
#include<stdio.h>
void swap(int *x,int *y);
void swap(int *x,int *y){
int temp;
printf("In swap 互换前x=%d,y=%d\n",*x,*y);
temp =*x;
*x=*y;
*y=temp;
printf("In swap 互换后x=%d,y=%d\n",*x,*y);
}
int main(){
int x=3,y=5;
swap(&x,&y);
printf("In main 互换后x=%d,y=%d\n",x,y);
return 0;
}
运行结果: