//求大神指教。。。。请问void changNum5函数交换地址不能交换存储的内容?
#include "stdio.h"
void changNum1(int x,int y);
void changNum2(int x,int y);
void changNum3(int *x,int *y);
void changNum4(int *x,int *y);
void changNum5(int *x,int *y);
int main(){
int a = 5;
// int *a1 = &a;
int b = 4;
// int *b1 = &b;
printf("交换前a = %d,b = %d\n",a,b);
printf("---------\n");
printf("a的地址是:%p\nb的地址是:%p\n",&a,&b);
printf("---------\n");
changNum1(a,b);
printf("交换后\na = %d,b = %d\n",a,b);
printf("a的地址是:%p\nb的地址是:%p\n",&a,&b);
changNum2(a,b);
printf("交换后\na = %d,b = %d\n",a,b);
printf("a的地址是:%p\nb的地址是:%p\n",&a,&b);
changNum3(&a,&b);
printf("交换后\na = %d,b = %d\n",a,b);
printf("a的地址是:%p\nb的地址是:%p\n",&a,&b);
changNum4(&a,&b);
printf("交换后\na = %d,b = %d\n",a,b);
printf("a的地址是:%p\nb的地址是:%p\n",&a,&b);
changNum5(&a,&b);
printf("交换后\na = %d,b = %d\n",a,b);
printf("a的地址是:%p\nb的地址是:%p\n",&a,&b);
return 0;
}
void changNum1(int x,int y){
int temp;
temp = x;
x = y;
y = temp;
}
void changNum2(int x,int y){
x = x + y;
y = x - y;
x = x - y;
}
void changNum3(int *x,int *y){
int temp = *x; // int *temp;
temp = *x; // temp=x;
*x = *y; // x=y;
*y = temp; // y=temp;
}
void changNum4(int *x,int *y){
*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
}
void changNum5(int *x,int *y){
int *temp;
temp=x;
x=y;
y=temp;
} |
|