Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

previous declaration of 'function' was here in C [duplicate]

Tags:

c

Possible Duplicate:
Learning C by K&R, error when trying to compile programs from book with arrays and function calls

While learning The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie, I tried the example in section 1.9 Character Arrays. Here are the codes:

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

There are two main errors:

  1. error: conflicting types for ‘getline’
  2. error: previous declaration of ‘getline’ was here

The complete error list is here:

/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

I am not sure what went wrong, since it is exactly the same code from the book. Maybe the declaration of functions at the beginning:

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

is problematic? Thank you!

like image 876
alittleboy Avatar asked Nov 04 '12 02:11

alittleboy


3 Answers

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

getline already exists in stdio.h. That is why you are getting the error. Change the function name to something else like getline_my.

Also, you are comparing a character with a string in line 16. It should be
if(c == '\n')

NOT

if(c == "\n")

like image 156
Neo Avatar answered Oct 03 '22 19:10

Neo


The problem is that there is likely a definition of getline in stdio.h. On my version of linux there is a getline function supplied by the C library (part of the POSIX standard I think). You can't have two functions with the same name in C, and this is your problem. Try renaming your version of getline to my_getline (both where you declare/define it and where you use it).

like image 32
CrazyCasta Avatar answered Oct 03 '22 20:10

CrazyCasta


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

It's exactly as it says: getline is declared in stdio.h (because the standard library provides a function with that name). You cannot provide your own function with that name, because when you call getline, the compiler wouldn't know which one to use.

like image 38
Karl Knechtel Avatar answered Oct 03 '22 19:10

Karl Knechtel