【问题标题】:C program issues with function prototype函数原型的 C 程序问题
【发布时间】:2014-05-27 06:07:23
【问题描述】:

我正在阅读 Denis 编写的 C 程序书,并且正在练习他的示例。我自己尝试了这个例子,然后从书中复制粘贴了相同的例子。我收到以下错误。

代码:

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

错误:

ex16.c:4:5: error: conflicting types for 'getline'
int getline(char line[], int maxline);
    ^
/usr/include/stdio.h:440:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
        ^
ex16.c:8:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^~~~
ex16.c:16:40: error: too few arguments to function call, expected 3, have 2
    while ((len = getline(line, MAXLINE)) > 0)
                  ~~~~~~~              ^
/usr/include/stdio.h:440:1: note: 'getline' declared here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
^
ex16.c:27:5: error: conflicting types for 'getline'
int getline(char s[], int lim)
    ^
/usr/include/stdio.h:440:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
        ^
1 warning and 3 errors generated.

只有我猜我正确地使用了函数原型。还参考了其他互联网资源。我不确定是不是因为编译器。我猜我正在使用 Gcc 4.3 版。操作系统 - mac 小牛。

你能帮帮我吗?

谢谢。

【问题讨论】:

  • 是的,它已经在那里了吗?函数原型是预先声明我们将使用哪些函数。你能用一个例子分享吗?请。
  • 你的意思是“getline”已经在 stdio.h 中声明了?嗯,让我尝试重命名它。
  • 谢谢。我从来不知道 getline 已经在 stdio.h 中声明了。多谢。它解决了问题。
  • @Bala 不,stdio.h 中没有这样的功能。看我的回答。
  • @Bala 将-std=c89 用于本书中的程序。

标签: c


【解决方案1】:

只需在您声明、定义和使用它的地方调用您的函数getlineMy()

getline() 已在 stdio.h (link) 中声明,并且它的实现将链接到您的程序,因此您不能使用该名称,除非您使用技巧。

【讨论】:

  • stdio.h中没有这个函数,编译器不允许加一个。如果是,则不符合 C 标准。
  • 您的参考也无关紧要。你应该引用C标准,你不能,因为里面没有这样的功能。
  • @Lundin 如果我对 C 标准第 4.6 节的理解是正确的,那么在 stdio.h 中包含 getline 的实现仍然可以称为“符合标准的独立实现”(前提是它在任何地方都尊重 C 语义当然还有)。
  • @Virgile 正如here 所讨论的,除了标准明确列出的标识符外,不允许实现定义任何标识符。如果它添加了非标准扩展,它必须使用下划线前缀。从这个例子中我们可以看出,编译器的非标准 getline 函数会影响严格符合程序的行为,因此编译器不是 C 兼容的。 (很可能在没有 gcc -std 选项的情况下编译)。
  • @Lundin,是的,但是 独立 实现(与“托管”相对)只需要提供标准头的一个(非常受限的)子集,并且@ 987654327@ 不属于此子集。因此,我倾向于说这样的实现可以用 stdio.h 做任何它喜欢的事情(除了 POSIX 合规性可能意味着 stdio.h 是 C 标准本身规定的严格超集)。
【解决方案2】:

您必须设置 GCC 才能将您的代码编译为 C。

gcc -std=c99 -pedantic-errors -Wall

然后你会得到一个错误:

8:1:错误:返回类型默认为 'int'

这是因为您对 main() 使用了错误的定义。更正为int main()

getline 这个名字很好用,在 stdio.h 和 the compiler is not allowed to add functions 中没有这样的函数,它们是该标头内的非标准扩展,没有用 _ 前缀命名它们。

【讨论】:

  • 由于他是从 K&R2 编译代码,-std=c89 将是首选
  • @MattMcNabb C89/C90 已经过时了 15 年。而且没有理由使用 25 年的标准,除非您使用的是 25 年的计算机。
  • 他正在编译根据 C89 标准编写的代码。
  • @MattMcNabb 是的,我知道。仍然没有理由这样做,他们不应该通过学习过时的书籍来学习 C。在这种情况下,C90 和 C99 之间的唯一区别是 main() 的显式 int。但是关于 stdio.h 和 getline 的主要问题并不依赖于 C 标准版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多