“函数”的冲突类型

时间:2021-04-05 09:11:58

标签: c

我正在通过“C 编程语言”一书学习 C。我被这个错误困住了。 代码如下:

  #include <stdio.h>
  #define MAXLINE 1000 // Max input line length

  int getline(char line[], int maxline);
  void copy(char to[], char from[]);
  // Printing the longest input line
  int main()
  {
    int len; // Current line length, max line length ever seen
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;
    while((len = getline(line, MAXLINE)) > 0)
    {
      if(len > max) // If current line length > previous max length - rewrite.
      {
        max = len;
        copy(longest, line);
      }
    }
    if(max > 0) // If there was a line
      printf("%s", longest);
    return 0;
  }

  int getline(char s[],int lim) // return length of line
  {
    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;
  }

这段代码是书中的一个例子:) 尝试在 Linux 上编译它:Linux 5.4.97-gentoo。 GCC 版本:10.2.0。

1 个答案:

答案 0 :(得分:1)

getline 已在 <stdio.h> 中声明。将方法名称更改为 my_getline

相关问题