首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么在编译K&R2的第一章中最长的行例时,我得到一个“getline的冲突类型”错误?

为什么在编译K&R2的第一章中最长的行例时,我得到一个“getline的冲突类型”错误?
EN

Stack Overflow用户
提问于 2012-01-07 03:07:11
回答 7查看 19.5K关注 0票数 39

这是我试图直接从“C编程语言”的1.9节中运行的程序。

代码语言:javascript
复制
#include <stdio.h>
#define MAXLINE 1000

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

main()
{
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

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


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


void copy(char to[], char from[])
{
    int i;

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

以下是我尝试使用Ubuntu 11.10编译程序时得到的错误:

代码语言:javascript
复制
cc     word.c   -o word
word.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
word.c:26:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
make: *** [word] Error 1

为了确保这本书中的印刷没有问题,我引用了本书(http://users.powernet.co.uk/eton/kandr2/krx1.html)中章后练习的这组答案,当我试图从该链接运行练习18、19、20、21等时,我得到了类似的错误。当我不能运行程序来查看它们的输出时,这真的很难学习。此问题是在一个程序中引入字符数组和函数调用时出现的。如果你对这个问题有任何建议,我将不胜感激。

EN

Stack Overflow用户

发布于 2012-01-07 03:10:56

这是因为stdio.h有一个getline()函数。

因此,实现此功能的简单方法是将函数重命名为my_getline()

getline()getdelim()最初都是GNU扩展。它们在POSIX.1-2008中是标准化的。

票数 10
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8763052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档