我正在尝试运行以下代码,该代码搜索行列表中最长的行并将其复制/保存。C语言中函数'getline'的冲突类型
int getline(char line[], int maxline);
void copy(char to[], char from[]);
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;
}
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;
}
但是,编译器说在getline
有一个错误,其中类型有冲突。
因此,主要是getline()
通过处理其中的字符数获取行列表中最长的行。
在问之前,请至少做一个关于warnig /错误信息的简单搜索!这比找到一个问题花费的时间更少。 – Olaf
BTW:getline(char s [],int lim)'中的拐角错误。考虑'getline(s,1)',if(c =='\ n')'会发生什么?最好使用'int c == 0;'。 – chux
[Stack Overflow用户需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) –