相关疑难解决方法(0)

在编译K&R2第1章中最长的行示例时,为什么会出现"getline的冲突类型"错误?

这是一个我试图直接从"C编程语言"第1.9节开始运行的程序.

#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 …
Run Code Online (Sandbox Code Playgroud)

c linux kernighan-and-ritchie

34
推荐指数
3
解决办法
2万
查看次数

符合ANSI C的实现是否可以在其标准库中包含其他功能?

是否符合ANSI C标准的实现允许在其标准库中包含其他类型和函数,超出标准列举的那些类型和函数?(理想的答案将参考ANSI标准的相关部分.)

我特别要求,因为Mac OS 10.7 getline在stdio.h中声明了该函数,即使使用该-ansi标志使用gcc或clang进行编译也是如此.这打破了几个定义自己getline功能的旧程序.这是Mac OS 10.7的错吗?(getlineMac OS 10.7上的手册页说明getline符合2008年发布的POSIX.1标准.)

编辑:为了澄清,我发现奇怪的是,在Mac OS 10.7上的ANSI C89程序中包含stdio.h也会引入该getline函数的声明,因为getline它不是K&R(可能是ANSI)中所列举的函数之一. stdio.h中.特别是,尝试编译noweb:

gcc -ansi -pedantic    -c -o notangle.o notangle.c
In file included from notangle.nw:28:
getline.h:4: error: conflicting types for ‘getline’
/usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here
Run Code Online (Sandbox Code Playgroud)

它是否是Mac OS 10.7中的一个错误,getline即使在编译ANSI C89标准时也包括stdio.h中的声明?

c standards ansi getline osx-lion

9
推荐指数
1
解决办法
1516
查看次数

如何在stdio.h中实现getline函数的自定义版本(CLANG,OS X)(答案:更改用于编译的POSIX标准)

晚上好,

我正在研究Kernighan和Ritchie的经典"C编程语言"中的练习.

在几个地方,练习可以创建自己的函数版本,该函数复制标准库中函数的名称.而不是为我的版本创建一个替代名称,我真的想告诉编译器我宁愿使用我的函数版本,然后使用标准库函数.

具体来说,如果我尝试编译一个解决方案来练习1-18,它从每行输入中删除尾随空格和制表符,我使用函数'getline'来读取stdin中的行.不幸的是,这会产生编译器错误,因为getline是在stdio.h中定义的.

我曾尝试使用#undef,但似乎无法使用它.

我已经搜索过其他类似的问题并找到了[这一个] [1]; 然而,它似乎需要黑客标准的库标题,我宁愿不这样做.

提前谢谢你的帮助.

这是代码(为了简短而删除了我的评论):

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 1000

static size_t getline(char s[], size_t lim) {

    char   c;
    size_t i = 0;

    while (--lim > 0 && (c = (char)getchar()) != (char)EOF && c != '\n')
        s[i++] = c;
    if (c == '\n')
        s[i++] = c;
    s[i] = '\0';

    return i;
}

int main(void) {

    char   line[MAXLINE] = "";
    size_t len = 0;

    while ((len = getline(line, MAXLINE)) > 0)
        if (len …
Run Code Online (Sandbox Code Playgroud)

c posix stdio clang getline

5
推荐指数
1
解决办法
352
查看次数

标签 统计

c ×3

getline ×2

ansi ×1

clang ×1

kernighan-and-ritchie ×1

linux ×1

osx-lion ×1

posix ×1

standards ×1

stdio ×1