The insert function is wrong or I'm passing something wrong during the main function? Basically, everytime I add a new node, it goes into the root of the tree, wich means the root is being NULL after the end of the insert function being executed by the repetition. Confirmed this by trying to print root->d in the main function after the insertion, and the program crashed.

To change the value of in one function of that in another you need its address. In other words, if you want to change the pointer to root in main you need to pass its address to insert, and you need to work with that address within insert.

To change the value of in one function of that in another you need its address. In other words, if you want to change the pointer to root in main you need to pass its address to insert, and you need to work with that address within insert.

We look up "man free" on the internet, aggressively removing "man freefalls".

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

Yes sir, it says nothing about changing memory in space that legitimately belongs to your program.

However, setting to NULL the value of freed pointers NULL is such a great idea that my personal c toolbox includes a wrapper on free that sets the pointer to NULL. If I compile without turning off debugging then of course my allocation and free routines display their activity and I can check for memory leak.