c语言 最大值问题。
#include<stdio.h>void main()
{
int *p1,*p2,*p3,a,b,c;
int swap(int x,int y); //swap实现变换
printf("please input 3 numbers:\n");
scanf("%d%d%d",&a,&b,&c);
p1 = &a;
p2 = &b;
p3 = &c;
if(*p1 < *p2)
{
swap(*p1,*p2);
}
else if(*p2 < *p3)
{
swap(*p2,*p3);
}
else if(*p1 < *p2)
{
swap(*p1,*p2);
}
printf("%d >= %d >=%d \n",*p1,*p2,*p3);
}
int swap(int *p1,int *p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
printf("please wait");
}
D:\C\swap\1.c(26) : warning C4028: formal parameter 1 different from declaration
D:\C\swap\1.c(26) : warning C4028: formal parameter 2 different from declaration
D:\C\swap\1.c(32) : warning C4716: 'swap' : must return a value
Linking...
LINK : fatal error LNK1168: cannot open Debug/swap.exe for writing
执行 link.exe 时出错.
swap.exe - 1 error(s), 0 warning(s)
照着打的为啥会报错呢,我写的就是两个指针吧。显示的是不是使用和声明的形式不一样呀。
还有函数必须有返回值吗,我做一个两个数变换可不可以呢,求大神解答。感谢! 你在main中声明的swap形参少了*
函数当然可以没有返回值,这样前面写 void,就不用写return了 #include<stdio.h>
void main()
{
int *p1,*p2,*p3,a,b,c;
int swap(int *x,int *y); //swap实现变换
printf("please input 3 numbers:\n");
scanf("%d%d%d",&a,&b,&c);
p1 = &a;
p2 = &b;
p3 = &c;
if(*p1 < *p2)
{
swap(*p1,*p2);
}
else if(*p2 < *p3)
{
swap(*p2,*p3);
}
else if(*p1 < *p2)
{
swap(*p1,*p2);
}
printf("%d >= %d >=%d \n",*p1,*p2,*p3);
}
void int swap(int *p1,int *p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
printf("please wait");
}
--------------------Configuration: swap - Win32 Debug--------------------
Compiling...
1.c
D:\C\swap\1.c(13) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
D:\C\swap\1.c(13) : warning C4024: 'swap' : different types for formal and actual parameter 1
D:\C\swap\1.c(13) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
D:\C\swap\1.c(13) : warning C4024: 'swap' : different types for formal and actual parameter 2
D:\C\swap\1.c(17) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
D:\C\swap\1.c(17) : warning C4024: 'swap' : different types for formal and actual parameter 1
D:\C\swap\1.c(17) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
D:\C\swap\1.c(17) : warning C4024: 'swap' : different types for formal and actual parameter 2
D:\C\swap\1.c(21) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
D:\C\swap\1.c(21) : warning C4024: 'swap' : different types for formal and actual parameter 1
D:\C\swap\1.c(21) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
D:\C\swap\1.c(21) : warning C4024: 'swap' : different types for formal and actual parameter 2
D:\C\swap\1.c(25) : error C2120: 'void' illegal with all types
D:\C\swap\1.c(26) : error C2371: 'swap' : redefinition; different basic types
D:\C\swap\1.c(5) : see declaration of 'swap'
执行 cl.exe 时出错.
swap.exe - 1 error(s), 0 warning(s)
麻烦大佬再帮忙看一下,为啥我 会出现这种错误,是int的对象不对吗?我理解的是*p1 ,就是地址p1的数值,跟实际数字一个意思呀, ahr123 发表于 2018-8-13 09:10
#include
void main()
{
#include<stdio.h>
void swap(int *x,int *y); // 类型要一样,声明最好放外面
int main(void) //建议改成 int,对调试有帮助
{
int *p1,*p2,*p3, a, b, c;
printf("please input 3 numbers:\n");
scanf("%d %d %d",&a,&b,&c); // 多重输入,最好有空格 %d %d 之间
p1 = &a;
p2 = &b;
p3 = &c;
if(*p1 < *p2)
{
swap(p1, p2); // 要求是指针类型,不需要加 *,加了变整型
}
else if(*p2 < *p3)
{
swap(p2, p3);
}
else if(*p1 < *p2)
{
swap(p1, p2);
}
printf("%d >= %d >=%d \n",*p1,*p2,*p3);
return 0; // int 配合 return 0
}
void swap(int *p1,int *p2) // 类型要一样,压根没有 return 所以用 void
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
printf("please wait");
}
你设计的程序还不完整,加油。输入 1 2 3 试试 已解决,感谢大佬。
另外刚看到指针,路漫漫其修远兮
页:
[1]