#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/06/24 08:38:31
![#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的值没有交换?](/uploads/image/z/15134098-58-8.jpg?t=%23includevoid+swap%28int+x%2Cint+y%29%7B+int+temp%3Btemp%3Dx%3Bx%3Dy%3By%3Dtemp%3B%7Dvoid+main%28%29%7Bint+a%2Cb%3Bscanf%28%E2%80%9C%25d%25d%E2%80%9D%2C%26a%2C%26b%29%3Bif%28a%3Eb%29swap%28a%2Cb%29%3Bprintf%28%E2%80%9C%25d%5Ct+%25d%E2%80%9D%2Ca%2Cb%29%3B%7D%E4%B8%BA%E4%BB%80%E4%B9%88a%2Cb%E7%9A%84%E5%80%BC%E6%B2%A1%E6%9C%89%E4%BA%A4%E6%8D%A2%3F)
#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);
}