C语言里面的nt temp; temp=*p1; *p1=*p2; *p2=temp; } temp是表示什么,给*p1,p2赋值做什么

#include<stdio.h>
void main()
{
void swap(int*p1,int*p2);
int a,b;
int*pointer_1,*pointer_2;
scanf("%d,%d",&a,&b);
pointer_1=&a;pointer_2=&b;
if(a<b)swap(pointer_1,pointer_2);
printf("\n%d,%d\n",a,b);
}
void swap(int*p1,int*p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}

temp是一个临时变量,用于存储变量的临时值,在这里,用于在交换两个变量值时,临时存储用。

void swap(int*p1,int*p2)
{
int temp;    //定义临时变量
temp=*p1;    //变量*p1的值存入temp,将*p1的值做个缓冲
*p1=*p2;     //变量*p2的值存入*p1
*p2=temp;    //最后将temp(原来*p1的值)存入*p2,此时*p1和*p2的值就相互交换
}


如果满意,欢迎采纳,谢谢

如有疑问欢迎追问

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-29
temp就是个临时变量,自定义函数swap()的作用是将p1指针指向的内容与p2指针指向的内容进行互换。
第2个回答  2014-07-29
这么理解吧,有两个桶,分别装满两桶水。
要求是把两个桶里的水交换一个, 有另外一个空桶可以使用。