could you please tell me what is wrong with this code it is compiling but a execution error occured i am unable to find that

am using devc++ compiler

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct link
 {
	int value;
	struct link * lchild;
	struct link *rchild;
  }node;

  node *head =NULL;
  node* create(node* ,int );
  void traverse(node *);
 // int height(node *);
int main()
 {
	 int n,i,val;
	 printf("enter how many node you want to create\n" );
	 scanf("%d",&n);
	 for( i=0; i<n; i++)
	  {
       printf("enter ur node value\n");
		 scanf("%d" ,&val);
		 head = create(head,val);
	  }
	  traverse(head);
	 //height(head);
	return 0;
  }

 node* create( node *tree ,int val)
  {
	  
    if(tree==NULL)
	  {
		tree= (node*)malloc(sizeof(node *));
		tree->value=val;
		tree->rchild=NULL;
		tree->lchild=NULL;
	  }
	 else
	   if(val>tree->value)
		  tree->rchild = create(tree->rchild,val);
	   else
	    if(val<tree->value)
          tree->lchild=create(tree->lchild, val);
	    else
         if(val==tree->value)
          printf("duplicate value");
	  }
  return tree; //here it says there is syntax error
}
void traverse (node *tree)
 {
	if(tree!=NULL)
	 {
	  traverse(tree->lchild);
	  printf(tree->value);
	  traverse(tree->rchild);
	 }
  }

> here it says there is syntax error
Because due to your poor indentation, the return statement is actually outside the function.

> tree= (node*)malloc(sizeof(node *));
1. Don't cast the result of malloc in C
2. you have the wrong sizeof - it's the size of the object, not the size of the pointer you want.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1047673478&id=1043284351

> printf(tree->value);
You really need a better compiler if you didn't get a warning from this use of printf.
http://en.wikipedia.org/wiki/Format_string_attack

> here it says there is syntax error
Because due to your poor indentation, the return statement is actually outside the function.

> tree= (node*)malloc(sizeof(node *));
1. Don't cast the result of malloc in C
2. you have the wrong sizeof - it's the size of the object, not the size of the pointer you want.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1047673478&id=1043284351

> printf(tree->value);
You really need a better compiler if you didn't get a warning from this use of printf.
http://en.wikipedia.org/wiki/Format_string_attack

could you please correct these errors in my code i have removed both things you said

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct link
 {
	int value;
	struct link * lchild;
	struct link *rchild;
  }node;

  node *head =NULL;
  node* create(node* ,int );
  void traverse(node *);
 // int height(node *);
int main()
 {
	 int n,i,val;
	 printf("enter how many node you want to create\n" );
	 scanf("%d",&n);
	 printf("%d",sizeof(head));
      for( i=0; i<n; i++)
	  {
       printf("enter ur node value\n");
		 scanf("%d" ,&val);
		 head = create(head,val);
	  }
	  traverse(head);
	 //height(head);
	return 0;
  }

 node* create( node *tree ,int val)
  {
	  
    if(tree==NULL)
	  {
		tree= malloc(sizeof(head));
		tree->value=val;
		tree->rchild=NULL;
		tree->lchild=NULL;
	  }
	 else
	   if(val>tree->value)
		  tree->rchild = create(tree->rchild,val);
	   else
	    if(val<tree->value)
          tree->lchild=create(tree->lchild, val);
	    else
         if(val==tree->value)
         printf("duplicate value");
  return tree;
}
void traverse (node *tree)
 {
	if(tree!=NULL)
	 {
	  traverse(tree->lchild);
	  printf(tree->value);
	  traverse(tree->rchild);
	 }
  }

thankx buddy it works

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.