C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 02:53 AM
sakura66 sakura66 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 sakura66 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 50 sec
Reputation 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!

Reply With Quote
  #2  
Old November 5th, 2012, 07:31 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,785 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 21 m 8 sec
Reputation Power: 1569
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 ShortUnderstanding the C/C++ Preprocessor
Taming PythonA 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Please debug this

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap