#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Please debug this


    Code:
    #include<stdio.h>
    #include<stdlib.h>
     
     
    int CompareIntegerDefault(void *data1, void *data2)
    {
         
        if(*(int *)data1>*(int *)data2)
            return (1);
        else if(*(int *)data1==*(int *)data2)
            return (0);
        else
            return (-1);
    }
    typedef struct AVLTreeNode
    {
        void *data ;
        int balfact ;
         
        struct AVLTreeNode *left ;
        struct AVLTreeNode *right ;
    } AVLTreeNode,*AVLTreeNodePtr;
     
     
    typedef struct ContainerHandle
    {
        struct AVLTreeNode *AVLRoot;
        //int h;
    }ContainerHandle_t,*ContainerHandlePtr_t;
    int h;
    void buildtree (AVLTreeNodePtr *root, void *data  )
    {
        struct AVLTreeNode *node1, *node2 ;
     
     
        if ( !root )
        {
            (*root) = ( struct AVLTreeNode * ) malloc ( sizeof ( struct AVLTreeNode ) ) ;
            (*root) -> data = data ;
            (*root) -> left = NULL ;
            (*root) -> right = NULL ;
            (*root) -> balfact = 0 ;
            h=1;
            return ;
        }
       
    }
     
     
    ContainerHandlePtr_t List;
     
     
    int main()
    {
        int a;
        List=(ContainerHandlePtr_t)malloc(sizeof(ContainerHandle_t));
        List->AVLRoot=NULL;
        a=5;
        buildtree ( &(List->AVLRoot),&a);    
        printf("%d",*(int *)(List->AVLRoot->data));
        return 0;
    }
    It is not giving the expected result-5
    Please help me debug this. Thanks!
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    The problem is in the test for the validity of root in buildtree(). As it is, you are testing whether root is NULL, but you just assigned a value to root, namely, an uninitialized AVLTreeNodePtr. What you actually want to test is that pointer itself, so the correct test would be

    Code:
        if ( !*root )
    Mind you, this doesn't fix the problem; when I tried the code with just this change, the compiler complained that *(root) ->data was not a valid structure member. While I am not certain why this would be so, The workaround I found was to initialize a local pointer, set up all the data in it, and assign that pointer to *root:

    Code:
    void buildtree (AVLTreeNodePtr *root, void *data  )
    {
        struct AVLTreeNode *new_tree = NULL;
    
        if ( !*root )
        {
            new_tree = ( struct AVLTreeNode * ) malloc ( sizeof ( struct AVLTreeNode ) ) ;
            if (new_tree != NULL)
            {
                new_tree -> data = data ;
                new_tree -> left = NULL ;
                new_tree -> right = NULL ;
                new_tree -> balfact = 0 ;
                h=1;
                *root = new_tree ;
            }
        }
    }
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo