#include
#include
int main()
{
int a, b;
a = 5;
b = 9;
Swap( a, b );
printf("a=%d,b=%d",a,b);
return 0;
}
void Swap( int x, int y )
{
int temp;
temp = x;
x = y;
y = temp;
}

求大神详解:为什么这个不能交换两个数
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
13条回答 默认 最新
关注
1.会报错是因为你将函数定义在了函数的外部,因此在main函数中,应该对该函数进行声明;
2.你这样的函数调用方式(值传递)是不会改变主函数中的值得;应该采用地址传递;代码如下:#include<stdio.h> #include <stdlib.h> /*值传递的形式*/ void Swap( int x, int y ); void Swap01( int *x, int *y ) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a, b; a = 5; b = 9; puts ("----------------"); printf("a=%d,b=%d\n",a,b); Swap( a, b ); puts ("----------------"); Swap01 (&a,&b); printf("a=%d,b=%d\n",a,b); system ("pause"); return 0; } void Swap( int x, int y ) { int temp; temp = x; x = y; y = temp; printf ("a = %d,b = %d\n",x,y); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报