3
我到K & RC工作和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編譯代碼的方式有關。請幫我看看我做錯了什麼。
多煩人。非常感謝您的幫助! – Raeven
getline仍然不是標準庫的一部分 - 它開始作爲GNU擴展並在POSIX 2008中被選中 –
@NigelHarper:確實,試圖通過[multiple](http://www.csse.uwa。 edu.au/programming/ansic-library.html#stdio)[參考](https://en.wikipedia.org/wiki/Stdio.h)[網站](http://www.acm.uiuc.edu/ webmonkeys/book/c_guide/2.12.html)對於ANSI C沒有提及'getline'。 – voithos