#includevoid swap(int x,int y){ int temp;temp=x;x=y;y=temp;}void main(){int a,b;scanf(“%d%d”,&a,&b);if(a>b)swap(a,b);printf(“%d\t %d”,a,b);}为什么a,b的值没有交换?

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/05 17:08:51
#includevoid swap(int x,int y){ int temp;temp=x;x=y;y=temp;}void main(){int a,b;scanf(“%d%d”,&a,&b);if(a>b)swap(a,b);printf(“%d\t %d”,a,b);}为什么a,b的值没有交换?

#includevoid swap(int x,int y){ int temp;temp=x;x=y;y=temp;}void main(){int a,b;scanf(“%d%d”,&a,&b);if(a>b)swap(a,b);printf(“%d\t %d”,a,b);}为什么a,b的值没有交换?
#include
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
void main()
{
int a,b;
scanf(“%d%d”,&a,&b);
if(a>b)
swap(a,b);
printf(“%d\t %d”,a,b);
}
为什么a,b的值没有交换?

#includevoid swap(int x,int y){ int temp;temp=x;x=y;y=temp;}void main(){int a,b;scanf(“%d%d”,&a,&b);if(a>b)swap(a,b);printf(“%d\t %d”,a,b);}为什么a,b的值没有交换?
函数都是值传递的,形参值的改变,并不能改变实参的值,要想改变,传地址.
#include
void swap(int *x,int *y) //这里是指针
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void main()
{
int a,b;
scanf(“%d%d”,&a,&b);
if(a>b)
swap(&a,&b); //把ab的地址传过去.
printf(“%d\t %d”,a,b);
}