need some help with trees, basically I'm building a tree of people with names and phone numbers which will then be searched, however i keep having problems with the insert function which causes segmentation faults.
i fear this could be something wrong with my pointers, but some help would be very much appreciated
Code:
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
typedef struct {
char name[255];
char number[255];
} entry;
typedef struct tree {
entry *thisnode;
struct tree *left;
struct tree *right;
} Tree ;
Tree *makenode(entry *p, Tree *l, Tree *r ) {
Tree *t = malloc( sizeof( Tree ) ) ;
t->left = l;
t->right = r;
strcpy(t->thisnode->name, p->name);
strcpy(t->thisnode->number, p->number);
return t;
}
Tree *insert(Tree *root, entry *what ) {
if( root == NULL ) {
root = makenode(what, NULL, NULL);
} else if(strcmp(what->name, root->thisnode->name) < 0 ) {
root->left = insert( root->left, what ) ;
} else {
root->right = insert( root->right, what ) ;
}
return root ;
}
int main( void ) {
entry *e;
Tree *tree = NULL;
do {
scanf("%s", e->name);
if( strcmp(e->name, "." ) != 0 ) {
scanf("%s", e->number);
tree = insert(tree, e);
}
} while( strcmp( e->name, "." ) != 0 ) ;
return 0;
}