|
发表于 2014-8-14 01:15:44
|
显示全部楼层
#include <iostream>
void swap(int *a,int *b)
{
int *temp=0;
temp = a;
printf("%d %d %d\n",temp,a,b);
printf("%d %d %d\n",*temp,*a,*b);
a = b;
printf("%d %d %d\n",temp,a,b);
printf("%d %d %d\n",*temp,*a,*b);
b = temp;
printf("%d %d %d\n",temp,a,b);
printf("%d %d %d\n",*temp,*a,*b);
return;
}
int main()
{
int a=12,b=34;
swap(&a,&b);
printf("%d %d\n",a,b);
}
这样看的更直观一点,在swap函数的时候已经交换了,结果没返回到main里:lol::lol:
往后看C++里面有办法解决这个问题达到你说的效果。不过那样更麻烦,还是直接用这种吧。
void swap(int *a,int *b)
{
int temp;
temp = *a;
printf("%d %d %d\n",temp,a,b);
//printf("%d %d %d\n",*temp,*a,*b);
*a = *b;
printf("%d %d %d\n",temp,a,b);
//printf("%d %d %d\n",*temp,*a,*b);
*b = temp;
printf("%d %d %d\n",temp,a,b);
//printf("%d %d %d\n",*temp,*a,*b);
return;
} |
|