C中二进制搜索函数中的类型冲突

时间:2018-07-16 16:14:39

标签: c struct

我想编写一个计算C关键字的程序。这是我的代码:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define BUFSIZE 100
#define MAXWORD 100
#define NKEYS (sizeof keytab / sizeof(keytab[0]))

char buf[BUFSIZE];
int bufp = 0;

int getword(char *, int);
int binsearch(char *, struct key *, int);

struct key {
    char * word;
    int count;
} keytab[] = {
        { "auto", 0 },
        { "break", 0 },
        { "case", 0 },
        { "char", 0 },
        { "const", 0 },
        { "continue", 0 },
        { "default", 0 },
        { "do", 0 },
        { "double", 0 },
        { "else", 0 },
        { "enum", 0 },
        { "extern", 0 },
        { "float", 0 },
        { "for", 0 },
        { "goto", 0 },
        { "if", 0 },
        { "int", 0 },
        { "long", 0 },
        { "register", 0 },
        { "return", 0 },
        { "short", 0 },
        { "signed", 0 },
        { "sizeof", 0 },
        { "static", 0 },
        { "struct", 0 },
        { "switch", 0 },
        { "typedef", 0 },
        { "union", 0 },
        { "unsigned", 0 },
        { "void", 0 },
        { "volatile", 0 },
        { "while", 0 }
};

int main(void) {
    int n;
    char word[MAXWORD];

    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))
            if ((n = binsearch(word, keytab, NKEYS)) >= 0)
                keytab[n].count++;
    for (n = 0; n < NKEYS; n++)
        if (keytab[n].count > 0)
            printf("%4d %s\n",
            keytab[n].count, keytab[n].word);
    return 0;
}

int getch(void) {
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) {
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}

int getword(char * word, int lim) {
    int c, getch(void);
    void ungetch(int);
    char * w = word;

    while (isspace(c = getch()))
        ;
    if (c != EOF)
        *w++ = c;
    if (!isalpha(c)) {
        *w = '\0';
        return c;
    }
    for ( ; --lim > 0; w++) {
        if (!isalnum(*w = getch())) {
            ungetch(*w);
            break;
        }
    }
    *w = '\0';
    return word[0];
}

int binsearch(char * word, struct key tab[], int n) {
    int cond;
    int low, high, mid;

    low = 0;
    high = n - 1;
    while (low <= high) {
        mid = (low + high) / 2;
        if ((cond = strcmp(word, tab[mid].word)) < 0)
            high = mid - 1;
        else if (cond > 0)
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}

当我尝试编译程序时,出现以下错误:

/home/lynx/Downloads/clion-2018.1.5/bin/cmake/bin/cmake --build /home/lynx/CLionProjects/untitled5/cmake-build-debug --target untitled5 -- -j 2
Scanning dependencies of target untitled5
[ 50%] Building C object CMakeFiles/untitled5.dir/main.c.o
/home/lynx/CLionProjects/untitled5/main.c:13:30: warning: ‘struct key’ declared inside parameter list will not be visible outside of this definition or declaration
 int binsearch(char *, struct key *, int);
                              ^~~
/home/lynx/CLionProjects/untitled5/main.c: In function ‘main’:
/home/lynx/CLionProjects/untitled5/main.c:59:38: warning: passing argument 2 of ‘binsearch’ from incompatible pointer type [-Wincompatible-pointer-types]
             if ((n = binsearch(word, keytab, NKEYS)) >= 0)
                                      ^~~~~~
/home/lynx/CLionProjects/untitled5/main.c:13:5: note: expected ‘struct key *’ but argument is of type ‘struct key *’
 int binsearch(char *, struct key *, int);
     ^~~~~~~~~
/home/lynx/CLionProjects/untitled5/main.c: At top level:
/home/lynx/CLionProjects/untitled5/main.c:102:5: error: conflicting types for ‘binsearch’
 int binsearch(char * word, struct key tab[], int n) {
     ^~~~~~~~~
/home/lynx/CLionProjects/untitled5/main.c:13:5: note: previous declaration of ‘binsearch’ was here
 int binsearch(char *, struct key *, int);
     ^~~~~~~~~
make[3]: *** [CMakeFiles/untitled5.dir/build.make:63: CMakeFiles/untitled5.dir/main.c.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/untitled5.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/untitled5.dir/rule] Error 2
make: *** [Makefile:118: untitled5] Error 2

似乎binsearch函数中发生错误。我无法弄清楚什么是错误,因为声明和定义中的类型似乎兼容。我使用CMake在CLion中构建项目。谁能帮我吗?

1 个答案:

答案 0 :(得分:1)

在声明struct key之前移动binsearch的定义,或在其前面提供struct的前向声明。

struct key;
int getword(char *, int);
int binsearch(char *, struct key *, int);

会的。