沿路直走 发表于 2018-10-8 10:54:01

递归查找

#include "stdio.h"

int main(void)
{
        int c;
        int i,j;

        for(i=0; i<10; i++)
        {
                c = 2*i;
        }
       
        while(1)
        {
                printf("请输入要查找的数字:");
                scanf("%d",&j);
                Find(c,j,0,9);
        }
       
        return 0;
}

void Find(int *c,int data,int front,int rear)
{
        int middle;
        middle = (front + rear)/2;
       
        if(front > rear)
                printf("查找结束,未找到。\n");
        else
        {
                if(data == c)
                        printf("查找结束,结果为:c[%d]\n",middle);
                else if(data > c)
                {
                        Find(c,data,middle+1,rear);
                }
                else if(data < c)
                {
                        Find(c,data,front,middle-1);
                }
        }
       
}

claws0n 发表于 2018-10-8 11:35:08

void Find(int *c,int data,int front,int rear);
如果定义在后,main() 之前要声明

沿路直走 发表于 2018-10-9 08:45:15

claws0n 发表于 2018-10-8 11:35
void Find(int *c,int data,int front,int rear);
如果定义在后,main() 之前要声明

嗯,是这样。但是我用DevC++在没有声明的情况下也可以正常执行,不知道为什么?难道是编译器自动声明了?

沿路直走 发表于 2018-10-9 09:03:16

沿路直走 发表于 2018-10-9 08:45
嗯,是这样。但是我用DevC++在没有声明的情况下也可以正常执行,不知道为什么?难道是编译器自动声明了?

作者:RednaxelaFX
链接:https://www.zhihu.com/question/35890756/answer/64907684
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

首先要知道Dev-C++只是一个IDE。它并不自己实现编译器,而是默认搭配MinGW版GCC编译器。接下来只要看这个问题的高票回答即可:Implicit function declarations in CIt should be considered an error. But C is an ancient language, so it's only a warning.Compiling with -Werror (gcc) fixes this problem.When C doesn't find a declaration, it assumes this implicit declaration: int f();, which means the function can receive whatever you give it, and returns an integer. If this happens to be close enough (and in case of printf, it is), then things can work. In some cases (e.g. the function actually returns a pointer, and pointers are larger than ints), it may cause real trouble.Note that this was fixed in newer C standards (C99, C11). In these standards, this is an error. However, gcc doesn't implement these standards by default, so you still get the warning.(大家似乎打不开那个链接所以把主要内容复制了过来。)GCC只是默认还允许implicit function declaration功能而已,较新的C规范(C99、C11)是不允许不声明直接用的。

claws0n 发表于 2018-10-9 11:04:54

沿路直走 发表于 2018-10-9 08:45
嗯,是这样。但是我用DevC++在没有声明的情况下也可以正常执行,不知道为什么?难道是编译器自动声明了?

dev c++ 呀~
In function 'main':
implicit declaration of function 'Find' [-Wimplicit-function-declaration]
At top level:
conflicting types for 'Find'//没有声明的情况,默认是 int,但你的函数是 void
previous implicit declaration of 'Find' was here

要看编译器,是可以运行,但是你到 VS 去看看,你这些不良习惯会让你哭!
你是程序员,要控制自己的代码,而不是让编译器随便帮你自动添加一些有的没的
也许你会认为这些是小问题,但假设说你有三五年经验了,这种低级错误是你不会除错的,因为你会仰赖于编译器帮你自动添加。那人家就会怀疑你是否真的有三五年经验。
#include <stdio.h>//标准库用尖括号

沿路直走 发表于 2018-10-9 20:29:14

claws0n 发表于 2018-10-9 11:04
dev c++ 呀~
In function 'main':
implicit declaration of function 'Find' [-Wimplicit-fu ...

的确,代码规范还得靠自己,受教了,以后多多注意。
页: [1]
查看完整版本: 递归查找