鱼C论坛

 找回密码
 立即注册
查看: 3351|回复: 4

[已解决]c语言 最大值问题。

[复制链接]
发表于 2018-8-13 08:57:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#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)
照着打的为啥会报错呢,我写的就是两个指针吧。显示的是不是使用和声明的形式不一样呀。

还有函数必须有返回值吗,我做一个两个数变换可不可以呢,求大神解答。感谢!
最佳答案
2018-8-13 12:08:46
ahr123 发表于 2018-8-13 09:10
#include
void main()
{
  1. #include<stdio.h>

  2. void swap(int *x,int *y);   // 类型要一样,声明最好放外面

  3. int main(void)          //建议改成 int,对调试有帮助
  4. {
  5.     int *p1,*p2,*p3, a, b, c;
  6.    
  7.     printf("please input 3 numbers:\n");
  8.     scanf("%d %d %d",&a,&b,&c);          // 多重输入,最好有空格 %d %d 之间
  9.    
  10.     p1 = &a;
  11.     p2 = &b;
  12.     p3 = &c;
  13.    
  14.     if(*p1 < *p2)
  15.     {
  16.         swap(p1, p2);       // 要求是指针类型,不需要加 *,加了变整型
  17.     }
  18.     else if(*p2 < *p3)
  19.     {
  20.         swap(p2, p3);
  21.     }
  22.     else if(*p1 < *p2)
  23.     {
  24.         swap(p1, p2);
  25.     }
  26.     printf("%d >= %d >=%d \n",*p1,*p2,*p3);
  27.    
  28.     return 0;             // int 配合 return 0
  29. }

  30. void swap(int *p1,int *p2)        // 类型要一样,压根没有 return 所以用 void
  31. {
  32.     int t;
  33.     t = *p1;
  34.     *p1 = *p2;
  35.     *p2 = t;
  36.     printf("please wait");
  37. }
复制代码


你设计的程序还不完整,加油。输入 1 2 3 试试
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-8-13 09:02:11 | 显示全部楼层
你在main中声明的swap形参少了*

函数当然可以没有返回值,这样前面写 void,就不用写return了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-13 09:10:28 | 显示全部楼层
#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的数值,跟实际数字一个意思呀,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-13 12:08:46 | 显示全部楼层    本楼为最佳答案   
ahr123 发表于 2018-8-13 09:10
#include
void main()
{
  1. #include<stdio.h>

  2. void swap(int *x,int *y);   // 类型要一样,声明最好放外面

  3. int main(void)          //建议改成 int,对调试有帮助
  4. {
  5.     int *p1,*p2,*p3, a, b, c;
  6.    
  7.     printf("please input 3 numbers:\n");
  8.     scanf("%d %d %d",&a,&b,&c);          // 多重输入,最好有空格 %d %d 之间
  9.    
  10.     p1 = &a;
  11.     p2 = &b;
  12.     p3 = &c;
  13.    
  14.     if(*p1 < *p2)
  15.     {
  16.         swap(p1, p2);       // 要求是指针类型,不需要加 *,加了变整型
  17.     }
  18.     else if(*p2 < *p3)
  19.     {
  20.         swap(p2, p3);
  21.     }
  22.     else if(*p1 < *p2)
  23.     {
  24.         swap(p1, p2);
  25.     }
  26.     printf("%d >= %d >=%d \n",*p1,*p2,*p3);
  27.    
  28.     return 0;             // int 配合 return 0
  29. }

  30. void swap(int *p1,int *p2)        // 类型要一样,压根没有 return 所以用 void
  31. {
  32.     int t;
  33.     t = *p1;
  34.     *p1 = *p2;
  35.     *p2 = t;
  36.     printf("please wait");
  37. }
复制代码


你设计的程序还不完整,加油。输入 1 2 3 试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-13 17:40:26 | 显示全部楼层
已解决,感谢大佬。
另外刚看到指针,路漫漫其修远兮
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 23:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表