这是我试图直接从“C编程语言”的1.9节中运行的程序。
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0)
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;
}以下是我尝试使用Ubuntu 11.10编译程序时得到的错误:
cc word.c -o word
word.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
word.c:26:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
make: *** [word] Error 1为了确保这本书中的印刷没有问题,我引用了本书(http://users.powernet.co.uk/eton/kandr2/krx1.html)中章后练习的这组答案,当我试图从该链接运行练习18、19、20、21等时,我得到了类似的错误。当我不能运行程序来查看它们的输出时,这真的很难学习。此问题是在一个程序中引入字符数组和函数调用时出现的。如果你对这个问题有任何建议,我将不胜感激。
发布于 2012-01-07 03:10:56
这是因为stdio.h有一个getline()函数。
因此,实现此功能的简单方法是将函数重命名为my_getline()
getline()和getdelim()最初都是GNU扩展。它们在POSIX.1-2008中是标准化的。
https://stackoverflow.com/questions/8763052
复制相似问题