简体   繁体   English

冲突类型

[英]Conflicting Types

I'm working through K&R C and GCC continues to give me this error for example 1.9: 我正在通过K&R C和GCC继续给我这个错误,例如1.9:

arrays.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:675:20: note: previous declaration of ‘getline’ was here
arrays.c:27:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:675:20: note: previous declaration of ‘getline’ was here
make: *** [arrays] Error 1

My code is: 我的代码是:

#include <stdio.h>
#define MAXLINE 1000    /* maximum input line size */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print longest input line */
int main()
{
    int len;            /* current line length */
    int max;            /* maximum length seen so far */
    char line[MAXLINE];     /* current input line */
    char longest[MAXLINE];  /* longest line saved here */

    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0)    /* there was a line */
        printf("%s", longest);
    return 0;
}

/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
    int c, i;

    for (i=0; i<lim-1 && (c=getchar()) !=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
    int i;

    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

I realize that the error suggests some discrepancy between the 'getline' function prototype and 'getline' function definition. 我意识到错误表明'getline'函数原型和'getline'函数定义之间存在一些差异。 I've copied and pasted the same code from another person's question from here to check if I had made a typo. 我从这里复制并粘贴了另一个人的问题中的相同代码,以检查我是否输错了。 It returned the same error message. 它返回了相同的错误消息。 I don't know if I'm getting this error because K&R's code has become outdated or if it's something to do with how GCC compiles the code. 我不知道我是否收到此错误,因为K&R的代码已经过时或者它与GCC如何编译代码有关。 Please help me find what I am doing wrong. 请帮我找一下我做错了什么。

Rename your getline function to something else. getline函数重命名为其他函数。 You're having a naming error with the getline function as defined in stdio.h . 你在stdio.h定义的getline函数有一个命名错误。 Note that the error says: " /usr/include/stdio.h :675:20: note: previous declaration of 'getline' was here" 注意错误说:“/ usr /include/stdio.h:675:20 :注意:'getline'的先前声明在这里”

getline is defined in stdio.h with the following signature: getlinestdio.h定义,带有以下签名:

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

Because C has no namespacing, the declarations in stdio.h are copied verbatim during compilation, thus causing a type mismatch. 由于C没有命名空间,因此在编译期间会逐字复制stdio.h中的声明,从而导致类型不匹配。

I'm not sure of the history, but most likely getline was not part of the standard library at the time K&R was written (in fact, as pointed out by @NigelHarper, it still isn't part of the C standard; it's part of POSIX). 我不确定历史,但很可能getline不是K&R编写时标准库的一部分(事实上,正如@NigelHarper指出的那样,它仍然不是C标准的一部分;它是它的一部分POSIX)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM