Friday, 6 September 2013

problems with pointers in C [on hold]

problems with pointers in C [on hold]

this is part of my code:
typedef struct Tree_t* Tree;
typedef void* Element;
struct Tree_t{
Element data;
Tree next, prev, parent;
Tree firstSon;
cmpFunc cmp;
copyFunc cpy;
freeFunc fre;
printFunc print;
};
Tree add(Tree place, Element data){
Tree newTree = (Tree) malloc(sizeof(struct Tree_t));
if (newTree == NULL) return NULL;
/*from here start the problem*/
newTree->parent = place;
newTree->print = place->print;
newTree->cmp = place->cmp;
newTree->cpy = place->cpy;
newTree->data = newTree->cpy(data);
newTree->fre = place->fre;
/*until here*/
if(place->firstSon == NULL){
place->firstSon = newTree;
newTree->next = NULL;
newTree->prev = NULL;
}else{
Tree curr = place->firstSon;
while(curr->next!=NULL && place->cmp(newTree->data,
curr->data)>0){
curr = curr->next;
}
if(curr->prev==NULL)
place->firstSon = newTree;
newTree->prev=curr;
newTree->next=curr->next;
if(curr->next!=NULL)
curr->next->prev = newTree;
}
return newTree;
}
i have a run time error while running this code. I marked the place in the
code where i think the problem is (if i ran the code without the marked
part it's OK) can someone tell me what is the problem? I think it's in the
parent pointer

No comments:

Post a Comment