The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Please debug this
Discuss Please debug this in the C Programming forum on Dev Shed. Please debug this C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 5th, 2012, 02:53 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
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!
|

November 5th, 2012, 07:31 AM
|
 |
Commie Mutant Traitor
|
|
Join Date: Jun 2004
Location: Norcross, GA (again)
|
|
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
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 ;
}
}
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|