Thread: conflicting types for getline() fix

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    conflicting types for getline() fix

    I am trying to compile a small program on linux. Here is the code.

    Code:
    #include <stdio.h>
    
    #define MAXLINE 1000
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    int 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("%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;
    		++i;
    	}
    	s[i] = '\0';
    	return i;
    }
    
    void copy(char to[], char from[]){
    	int i;
    
    	i = 0;
    	while((to[i] = from[i]) != '\0')
    	++i;
    }
    and here are the errors that gcc gave me.

    line.c:5: error: conflicting types for ‘getline’
    /usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
    line.c:27: error: conflicting types for ‘getline’
    /usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
    line.c: In function ‘getline’:
    line.c:30: warning: comparison between pointer and integer


    I couldn't get error: conflicting types for getline fix.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure how it could get any clearer. It gives you line numbers, and tells you what's wrong:
    Code:
    int getline(char line[], int maxline);
    Code:
    int getline(char s[], int lim[]){

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    : ) Lols. How could I miss that. I saw that many times. I think I was not in a frame of mind then. Well, correcting that it still annoys me though.

    Code:
    #include <stdio.h>
    
    #define MAXLINE 1000
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    int 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("%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;
    		++i;
    	}
    	s[i] = '\0';
    	return i;
    }
    
    void copy(char to[], char from[]){
    	int i;
    
    	i = 0;
    	while((to[i] = from[i]) != '\0')
    	++i;
    }
    line.c:5: error: conflicting types for ‘getline’
    /usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
    line.c:27: error: conflicting types for ‘getline’
    /usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here


    What is wrong now?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    your stdio.h already has a declaration of getline and it is different from your own declaration - try to rename your function to avoid conflicts
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Thanks.
    Last edited by saqib_; 04-05-2010 at 04:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  2. Replies: 4
    Last Post: 01-06-2009, 02:08 AM
  3. What types of files can C++ file processing handle?
    By darsunt in forum C++ Programming
    Replies: 9
    Last Post: 10-28-2008, 11:33 AM
  4. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  5. Very angry: "two or more data types in declaration"
    By quzah in forum C Programming
    Replies: 2
    Last Post: 06-04-2003, 07:16 AM