2012-11-04 30 views
5

Possible Duplicate:
Learning C by K&R, error when trying to compile programs from book with arrays and function calls以前的“功能”的声明,在这里用C

虽然学习由Brian W. Kernighan和丹尼斯·里奇M. C程序设计语言,我想在第1.9字符数组的例子。下面是代码:

/* read a set of text lines and print the longest */ 

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

/* declare functions: getline() and copy() */ 
int getline(char line[], int maxline); 
void copy(char to[], char from[]); 

/* getline: read a line into array "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'; /* the null character whose value is 0 */ 
    return i; 
} 

/* copy: copy 'from' into 'to'; assume to is big enough */ 
/* the return type of copy is "void" -- no value is returned */ 
void copy(char to[], char from[]) 
{ 
    int i; 
    i = 0; 
    while ((to[i] = from[i]) != '\0') /* terminated with a \0 */ 
     ++i; 
} 

/* print the 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; 
} 

主要有两种错误:

  1. 错误:冲突的类型为 '函数getline'
  2. 错误:的 '函数getline' 先前的声明是在这里

完整的错误列表在这里:

/Users/C/Codes/Ritchie/array_char.c:8: error: conflicting types for ‘getline’ 
/usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here 
/Users/C/Codes/Ritchie/array_char.c:13: error: conflicting types for ‘getline’ 
/usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here 
/Users/C/Codes/Ritchie/array_char.c: In function ‘getline’: 
/Users//C/Codes/Ritchie/array_char.c:17: warning: comparison between pointer and integer 
/Users/C/Codes/Ritchie/array_char.c:17: warning: comparison with string literal results in unspecified behavior 

我不确定出了什么问题,因为它与本书中的代码完全相同。也许功能在开头的声明:

int getline(char line[], int maxline); 
void copy(char to[], char from[]); 

有问题吗?谢谢!

+1

请发布实际的错误消息(不要释义) – CrazyCasta

+0

是否有理由在这里完成所有困难的方式,或者您可以使用'fgets'和'strcpy'来代替? –

回答

8

http://www.kernel.org/doc/man-pages/online/pages/man3/getline.3.html

函数getline已经存在stdio.h中。这就是你遇到错误的原因。将函数名称更改为getline_my之类的其他名称。

而且,你是比较字符与线16字符串应该是
if(c == '\n')

if(c == "\n")

+0

好斑点!我猜OP输入了代码。 –

+0

OP的OP输入代码。他正在阅读一本书:P – CrazyCasta

+0

@Neo:感谢您指出这一点! – alittleboy

4

问题是stdio.h中可能有getline的定义。在我的Linux版本中,有一个由C库提供的getline函数(我认为是POSIX标准的一部分)。你不能在C中有两个同名的函数,这是你的问题。尝试将您的版本getline重命名为my_getline(您声明/定义它以及在何处使用它)。

1

从年月日时本书是写了时下的标准C库稍作修改,并且它们不再与旧的和新的一致。

您必须删除声明,并从当前的stdio.h中保留声明。

+0

这不是从标准的变化。它来自于gcc未能符合标准。 –

+0

他可能正在阅读第二版,该版已更新为ANSI C,应在任何C编译器上进行编译。 (另外,他写的代码应该在任何C编译器上运行,除了函数名已被使用,因此他需要更改它)。 – CrazyCasta

+0

是的,当前gcc默认为c89,除非你插入-c99。 c11也出现了。 – alinsoar

2
/usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here 

这正是因为它说:getlinestdio.h声明(因为标准库提供该名称的功能)。你不能用这个名字提供你自己的函数,因为当你调用getline时,编译器不知道使用哪一个函数。