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'

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

p=talloc();

According to the forward declaration, talloc returns a pointer to a node, but p is a pointer to a tnode.

treeprintf(p->right);
No such function named treeprintf

struct tnode *talloc(void)
Disagrees with the previous forward declaration, struct node *talloc(void);

Essentially, all your errors are typing mistakes.

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...

try this.

printf ("%d\n", EOF);

try this.

printf ("%d\n", EOF);

That's how you find out what the value of EOF is, but it's useless information. The value of EOF doesn't correspond to a key code, and can vary between compilers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.