help with trees

This is a discussion on help with trees within the C Programming forums, part of the General Programming Boards category; need some help with trees, basically I'm building a tree of people with names and phone numbers which will then ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    help with trees

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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'e' is just a pointer. It doesn't point to anything, and you aren't allocating any space for it.


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

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    yeah i've tried e as both a pointer and not a pointer, it still has the same pointer

    edit: maybe not, this seems to have fixed it

    cheers
    Last edited by dacbo; 12-06-2005 at 02:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trees
    By masterg in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2008, 01:42 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Trees
    By samtay in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2004, 09:35 AM
  4. Binary Trees...
    By SirCrono6 in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2003, 04:54 PM
  5. AVL Trees
    By kwigibo in forum C Programming
    Replies: 2
    Last Post: 04-17-2002, 06:46 PM

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