一个C语言将换两个数的问题
来源:百度知道 编辑:UC知道 时间:2024/07/15 03:36:31
#include <stdio.h>
#include <stdlib.h>
int main()
{
/*将换两个数*/
int a=1,b=2;
void swap(int &a, int &b);
swap(a,b);
printf("%d %d",a,b);
system("pause");
return 0;
}
void swap(int &a,int &b)
{
/*代码省略*/
}
上面的代码总是报错~~~真不知道错在哪里了,哪位高手指点一下
#include <stdlib.h>
int main()
{
/*将换两个数*/
int a=1,b=2;
void swap(int &a, int &b);
swap(a,b);
printf("%d %d",a,b);
system("pause");
return 0;
}
void swap(int &a,int &b)
{
/*代码省略*/
}
上面的代码总是报错~~~真不知道错在哪里了,哪位高手指点一下
用c++编译器,c里面没有引用(&)
“int a=1,b=2;
void swap(int &a, int &b);”
明显a和b重定义了(定义了两次)。
将void swap(int &a, int &b)中的形参换成int &x, int &y.
swap函数拿到main函数前边声明去。顺便说下。lz的int&a没啥错,这是引用传递,不过貌似c++才有的,好有才。。。。
我也是菜鸟 改了下 能换了 自己看吧 我怀疑你把函数声明的参数意义理解错了 声明传入的是指针 你就该写 Int *a
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=1,b=2;
void swap( int *a, int *b);
swap(&a,&b);
printf("%d %d",a,b);
system("pause");
return 0;
}
void swap(int *a,int *b)
{int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
把swap函数里面的符号&去掉试下