Can anyone....

This is a discussion on Can anyone.... within the C Programming forums, part of the General Programming Boards category; Can anyone please debug the following binary tree program, it flashes two errors: 1: Type mismatch in redeclaration of 'strdup' ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    Can anyone....

    Can anyone please debug the following binary tree program, it flashes two errors:
    1: Type mismatch in redeclaration of 'strdup'
    2: Tpe mismatch in redeclaration of 'talloc'

    Code:
     
        #include<stdio.h>
        #include<ctype.h>
        #include<string.h>
         
        #define MAXWORD 100
        struct tnode *addtree(struct tnode *, char *);
        void treeprint(struct tnode *);
         
        struct tnode
        {
            char *word;
            int count;
            struct tnode *left;
            struct tnode *right;
        };
      
      main()
        {
            struct tnode *root;
            char word[MAXWORD];
         
            root = NULL;
            while(gets(word)!= "\n")
            if(isalpha(word[0]))
            root=addtree(root, word);
            treeprint(root);
        }
         
        struct node *talloc(void);
        char *strdup(char *);
         
        struct tnode *addtree(struct tnode *p, char *w)
        {
            int cond;
         
            if (p==NULL)
            {
                  p=talloc();
                  p->word=strdup(w);
                  p->count=1;
                  p->left=p->right=NULL;
             }else if((cond=strcmp(w,p->word))==0)
                  p->count++;
             else if(cond < 0)
                  p->left = addtree(p->left,w);
             else
                  p->right = addtree(p->left,w);
          return p;
        }
         
        void treeprint(struct tnode *p)
        {
              if(p!=NULL)
        {
              treeprint(p->left);
              printf("%4d %s\n",p->count, p->word);
              treeprintf(p->right);
        }
        }
         
        #include<stdlib.h>
         
        struct tnode *talloc(void)
        {
                return (struct tnode *)malloc(sizeof(struct tnode));
        }
         
        char *strdup(char *s)
        {
            char *p;
            p=(char *)malloc(strlen(s)+1);
            if(p!=NULL)
            strcpy(p,s);
            return p;
        }
    Last edited by juice; 09-18-2011 at 08:12 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Indentation, learn it. Use it.
    2. main returns an int, and so you should have something like return 0; at the bottom of main.
    3. gets ... read the FAQ.
    4. Compare the prototype of talloc with the definition. Read very carefully.
    5. Why is there an include way down at the bottom of your file?


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

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    The declaration is in harmony with the defination. I can't find any error....

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    4. Compare the prototype of talloc with the definition. Read very carefully.
    Quote Originally Posted by juice View Post
    The declaration is in harmony with the defination. I can't find any error....
    There is a consonant missing, silly.

    Strdup is not a standard function, but very likely there is a function with that name in string.h. Better to use names like "my_strdup" for such stuff.

    Quote Originally Posted by quzah View Post
    1. Indentation, learn it. Use it.
    PLEASE PLEASE PLEASE. For your own good and everyone else's.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    Oh for crying out loud! I just spent the last 15 minutes trying to match what you guys were saying with the OP's code...
    Then I finally notice that he's edited the first message to fix the errors... What an incredily stupid and rude thing for him to do.
    S/he didn't edit the error out; there is a type mismatch for the declaration and definition of talloc() because of a letter missing in the former.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    S/he didn't edit the error out; there is a type mismatch for the declaration and definition of talloc() because of a letter missing in the former.
    Thanks... how many dozen times did I miss that? (Such a day this is going to be!)

    (offending post removed)

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Thanx Quzah, Thanx MK. Code is running now.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    In line 23 instead of "gets", I tried to use a function getword() which gets a word from the keyboard until EOF is encountered. What is this end of file (EOF) character, and how is it generated? I tried enter key, escape key, tab key, but none of them corresponds to EOF...

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by juice View Post
    In line 23 instead of "gets", I tried to use a function getword() which gets a word from the keyboard until EOF is encountered. What is this end of file (EOF) character, and how is it generated? I tried enter key, escape key, tab key, but none of them corresponds to EOF...
    You should start a new thread when you have a new question. Anyway, EOF is a special value (possibly -1, but don't count on it) the system passes to your program to indicate the possibility of input has terminated.

    You can simulate EOF on the console by using ctrl-d (*nix) or ctrl-z (windows). You may have to hit it twice in a row.

    BUT since you are looking for a word and terminating on the newline, the newline itself is a better thing to watch for -- most users will find typing a word and hitting enter, not ctrl-d a few times, easier.

    Code:
    #include <stdio.h>
    
    int readIn (char *buf, int max) {
    	int ch, i = 0;
    	max--;
    	while (1) {
    		ch = fgetc(stdin);
    		if (i == max || ch == EOF || ch == '\n') {
    			buf[i] = '\0';
    			return i;
    		}
    		buf[i++] = ch;
    	}
    }
    
    int main(int argc, const char *argv[]) {
    	char buf[256];
    	int n = readIn(buf, 256);
    
    	printf("%d: %s", n, buf);
    
    	return 0;
    }
    Of course, there is a standard function which does the same thing, and avoid the pitfalls of gets(), namely, fgets().
    Last edited by MK27; 09-18-2011 at 09:30 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21