我正在尝试运行以下代码,该代码搜索行列表中最长的行并复制/保存它。
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()
通过处理行列表中的字符数来获取行列表中最长的行。
最佳答案
它是冲突的,因为你重新定义了原始libc函数的原型(prototype)getline()
其原型(prototype)定义于 <stdio.h>
.
来自 man 3 getline
我们有:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
如果你用 int getline(char line[], int maxline)
重新定义它你与原来的getline()
相冲突功能。
只需重命名您的函数,使其不会与库冲突。
关于C语言中函数 'getline'的冲突类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45383668/