Help me please to find the problem in my code:

Here is the error that I'm getting:

serg@serg-PORTEGE-Z835:~/c$ gcc string_finder.c -o string_finder
string_finder.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
string_finder.c:22:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here

And here is the code:

#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int max);
int strindex(char source[], char searchfor[]);

char pattern[] = "ould";

int main() {

    char line[MAXLINE];
    int found = 0;

    while(getline(line, MAXLINE) > 0)
        if(strindex(line, pattern) >=0 ) {
            printf("%s", line);
            found++;
        }
    return found;
}

int getline(char s[], int lim) {

    int c, i;

    i = 0;
    while(--lim > 0 && (c=getchar()) != EOF && c != '\n')
        s[i++] = c;
    if(c == '\n')
        s[i++] = c;
    s[i] = '\0';
    return i;
}

int strindex(char s[], char t[]) {

    int i, j, k;

    for(i = 0; s[i] != '\0'; i++) {
        for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if(k > 0 && t[k] == '\0')
            return i;
    }
    return -1;
}

I believe there's already a builtin getline function in stdio.h
try to rename the function and all instance that uses the same name

This article has been dead for over six months. Start a new discussion instead.