elandr
elandr

Reputation: 3

Conflicting types with a declared int func

Reading Kernigan, and the given code doesn't seem to work because of 'conflicting types' error for the user function.

The compiler I'm using is cc 7.4.0. I've declared the function in the beginning and have been checking and re-checking the types seemingly forever. Guess I'm missing something.

#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int maxline);

main()
{
    int len;
    char line[MAXLINE];

    while((len = getline(line, MAXLINE)) > 0)
        ;
    return 0;
}

int getline(char s[], int lim)
{
    int i, c;

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

The error I'm getting is

func.c:4:5: error: conflicting types for ‘getline’
 int getline(char line[], int maxline);
     ^~~~~~~
In file included from func.c:1:0:
/usr/include/stdio.h:616:20: note: previous declaration of ‘getline’ was here
 extern _IO_ssize_t getline (char **__restrict __lineptr,
                    ^~~~~~~
 ^~~~
func.c:25:5: error: conflicting types for ‘getline’
 int getline(char s[], int lim)
     ^~~~~~~
In file included from func.c:1:0:
/usr/include/stdio.h:616:20: note: previous declaration of ‘getline’ was here
 extern _IO_ssize_t getline (char **__restrict __lineptr,

Upvotes: 0

Views: 118

Answers (2)

Bathsheba
Bathsheba

Reputation: 234785

getline is already declared (in error actually) in <stdio.h> and its signature differs from yours.

The simplest thing to do is to rename your version to something different.

Upvotes: 4

Lundin
Lundin

Reputation: 214300

You need to block the compiler from spewing non-standard crap into standard headers. You are using some compiler which has defined a non-standard getline function inside the standard library header stdio.h, which is non-conforming.

With gcc you should compile as for example gcc -std=c17 -pedantic-errors to prevent this from happening.

Upvotes: 2

Related Questions