Thread: conflicting types for built-in function

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55

    conflicting types for built-in function

    Im getting this error over my function prototype in the code below:

    Code:
    #include <stdio.h>
    
    void strcat(char s[], char t[]); /* error here */
    
    int main(void)
    {
    	char s[4] = {"abcd"};
    	char t[9] = {"abcdefgh"};
    
    	(strcat(s, t));
    
    	return 0;
    
    }
    
    void strcat(char s[], char t[]){
    	char match;
    	int i, j, x, q;
    	i = j = x = q = 0;
    	while(s[i] != '\0')
    		i++;
    	while(s[j] != '\0')
    		j++;
    	while(q <= i){
    	for(x = 0; x <= j; x++)
    		if(s[q] == t[x]){
    			match = 1;
    	}
    		else{
    			match = 0;
    	if(match == 1){
    		++q;
    	}
    	else{
    		t[j++] = s[q++];
    	}
    
    		}
    	}
    
    	int v = 0;
    	while(t[v] != '\0'){
    		putchar(t[v]);
    				v++;
    	}
    
    }

    Any ideas as to where im going wrong?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is a standard function called strcat with the following declaration: char *strcat(char *dest, const char *src);. Try naming your function my_strcat or some such.

    EDIT: Unless this is a purely academic exercise, there is no reason to reinvent the wheel.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Sweet. Thats solved that problem! Thanks!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Note that your code is wrong anyway. You're first off trying to put 5 characters('a','b','c','d','\0') into an array of 4 characters. Then after that you are trying to put even more chars into one of your buffers, causing yet more overflow.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    char s[4] = {"abcd"};
    You should note that this particular string does NOT have a \0 at the end.

    Normally, the safe thing to do is omit the size and let the compiler work it out.
    Code:
    char s[] = "abcd";
    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.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Thanks for the heads up guys! Wasn't aware that I could leave the array size initialisation to the compiler in this context! Have altered the code as you suggested. Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting types error
    By mastrxplodr in forum C Programming
    Replies: 4
    Last Post: 04-12-2011, 11:56 AM
  2. Conflicting Types for function
    By tomeatworld in forum C Programming
    Replies: 1
    Last Post: 12-06-2010, 11:43 AM
  3. Conflicting types???
    By kwikness in forum C Programming
    Replies: 11
    Last Post: 10-07-2007, 11:53 PM
  4. error: conflicting types
    By Rodman in forum C Programming
    Replies: 5
    Last Post: 03-10-2006, 04:20 AM
  5. conflicting types for...
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 07:03 AM