繁体   English   中英

冲突类型

[英]Conflicting Types

我正在通过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

我的代码是:

#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;
}

我意识到错误表明'getline'函数原型和'getline'函数定义之间存在一些差异。 我从这里复制并粘贴了另一个人的问题中的相同代码,以检查我是否输错了。 它返回了相同的错误消息。 我不知道我是否收到此错误,因为K&R的代码已经过时或者它与GCC如何编译代码有关。 请帮我找一下我做错了什么。

getline函数重命名为其他函数。 你在stdio.h定义的getline函数有一个命名错误。 注意错误说:“/ usr /include/stdio.h:675:20 :注意:'getline'的先前声明在这里”

getlinestdio.h定义,带有以下签名:

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

由于C没有命名空间,因此在编译期间会逐字复制stdio.h中的声明,从而导致类型不匹配。

我不确定历史,但很可能getline不是K&R编写时标准库的一部分(事实上,正如@NigelHarper指出的那样,它仍然不是C标准的一部分;它是它的一部分POSIX)。

暂无
暂无

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

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