I can't seem to find where the problem is in my code, i'm essentially storing character patterns, if an existing pattern is already present I'll store it in the duplicate node, otherwise it will shift to the regular pattern one. The problem starts when i have a duplicate, it should move to the duplicate node, but instead loops infinitely.
The binary tree struct: :
struct arbre{
char id[10];
int length;
int token;
int count;
struct arbre *pattern;
struct arbre *doublon;
};
typedef struct arbre *Arbre;
The function to create and make new nodes
void ajouter(Arbre *a, char *tablettre, int length, int token){
if(*a==NULL){
*a=(Arbre)malloc(sizeof(struct arbre));
strcpy((*a)->id, tablettre);
//append((*a)->id,tablettre);
//printf("%s",(*a)->id);
(*a)->length = length;
(*a)->token = token;
(*a)->doublon=NULL;
(*a)->pattern=NULL;
}
if (strcmp((*a)->id, tablettre) == 0){ /// The problem is here
printf("%s",(*a)->doublon->id);
printf(" and %s",tablettre);
ajouter(&(*a)->doublon, tablettre, length, token);
}
if (strcmp((*a)->id, tablettre) != 0){
ajouter(&(*a)->pattern, tablettre, length, token);
}
}
*a==NULL, what is content ofdoubloninprintf("%s",(*a)->doublon->id);?typedef struct arbre *Arbre;hiding pointer types with a typedefs only adds confusion. When I seeArbreI have absolutely no idea that is is a pointer. If I seestruct arbre *, I know it's a pointer.*a=(Arbre)malloc(sizeof(struct arbre));are terrible. Rather write(struct arbre*)malloc(sizeof(struct arbre));or remove the cast alltogether and write*a = malloc(sizeof(struct arbre));NULLpointer, how would you ever get out of your recursion once you pass*a==NULL? You will always find thatidholds same content astablettreas you just copied it there a few lines above. Then you will always call again. You mustn't go fromiftoifwithout anelseifafter you entered the first one. That would have shown the issue withing seconds.