c语言中用函数交换两个变量的值?

来源:百度知道 编辑:UC知道 时间:2024/05/11 15:21:41
程序输出是1,2 不是2,1
#include "Stdio.h"
#include "Conio.h"

void swap(int x,int y)
{ int t;
t=x;
x=y;
y=t;

}

main()
{ int a,b;
a=1,b=2;
swap(a,b);
printf("%d,%d",a,b);
getch();
}
添加了return x,y;输出还是1,2

#include "Stdio.h"
#include "Conio.h"

void swap(int* x,int *y)
{ int t;
t=*x;
*x=*y;
*y=t;

}

main()
{ int a,b;
a=1,b=2;
swap(&a,&b);
printf("%d,%d",a,b);
getch();
}

输入变量:x,y
#include<stdio.h>
void main()
{int x,y,*xp=&x,*yp=&y;
void f(int*xp,int*yp );
f(xp,yp);
}
void f(int*xp,int*yp )
{int a;
a=*xp;
*xp=*yp;
*yp=a;
}
调用函数后,x,y的值就被交换了

应该在void swap(int x,int y)
{ int t;
t=x;
x=y;
y=t;
return x,y;
}
里添加一句,如上