Thread: Compiler error

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Compiler error


    Hello I am going through the '"C Programming Language" book and tried this code right out of the text how ever I get this compiler error:

    ll.c:6:5: error: conflicting types for ‘getline’
    /usr/include/stdio.h:675:20: note: previous declaration of ‘getline’ was here
    ll.c:26:7: error: conflicting types for ‘getline’
    /usr/include/stdio.h:675:20: note: previous declaration of ‘getline’ was here

    I can't imagine that the code in this book is wrong and it look fine to me can anyone see the mistake in this code?

    // This program returns the longest line.

    #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("the longest line is %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++;
    }

    ~
    ~
    ~ :confused:
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Code:
           #include <stdio.h>
           ssize_t getline(char **lineptr, size_t *n, FILE *stream);
    getline is a POSIX standard function.

    Try renaming your function to be something else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by salem
    Code:
           #include <stdio.h>
           ssize_t getline(char **lineptr, size_t *n, FILE *stream);
    getline is a POSIX standard function.

    Try renaming your function to be something else.

    Thanks, that did it.

    Comments on this post

    • clifford agrees : ... but you should not close threads just because you have an answer. Closing is to prevent posting to threads that have degraded into unconstructive argument or abuse. Someone may have useful insight to add - I did.

IMN logo majestic logo threadwatch logo seochat tools logo