title: How do I remove this segmentation fault in my bst_insert_node function?
body: <p>I am having a segmentation fault in my function <code>bst_insert_node()</code>. Left is the left "child" of the node which always has a smaller phone value than it's parent. Right is the right "child" of the node which always has a greater phone value than it's parent. Bst stands for binary search tree. My main function is inside another file. The main problem should be my function <code>bst_insert_node()</code>.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "introprog_telefonbuch.h"
  
  // Adds a node with the given phone number (phone) and the name of the owner of that phone number
  // into the binary search tree.
  
  void bst_insert_node(bstree* bst, unsigned long phone, char *name) {
  
  bst_node* new = malloc(sizeof(bst_node));
  name = malloc(sizeof(char) *MAX_STR);
  snprintf(name, MAX_STR, "%s",name);
  new-&gt;left = NULL;
  new-&gt;right = NULL;
  new-&gt;parent = NULL;
  new-&gt;phone = phone;
  
  if(bst == NULL){
  bst-&gt;root = new;
  }
  
  bst_node* finder = bst-&gt;root;
  
  while(finder != NULL){
  if(finder-&gt;phone == phone){
    printf("This phone number already exist");
    return;
  }
  if(phone &lt; finder-&gt;phone){
    if(finder-&gt;left == NULL){
      new-&gt;parent = finder;
      finder-&gt;left = new;
      return;
    }
    finder = finder-&gt;left;
  }
  
  
  if(phone &gt; finder-&gt;phone){
    if(finder-&gt;right == NULL){
      new-&gt;parent = finder;
      finder-&gt;right = new;
      return;
    }
  
    finder = finder-&gt;right;
  }
  }
  }
  
  // This function returns a pointer that points to the node that contains the phone
  // number that is being searched. If such a phone number is non existent,
  // you return NULL.
  bst_node* find_node(bstree* bst, unsigned long phone) {
  
  bst_node* finder = bst-&gt;root;
  
  while(finder != NULL){                       
  
  if(finder-&gt;phone == phone){ 
      return finder;
  }
  
  if(finder-&gt;phone &gt; phone){
      finder = finder-&gt;left;
  }  
  else{
      finder = finder-&gt;right;
  } 
  
   }
    return NULL;
  }
  
  // print a sub-tree in "in-order" order
  void bst_in_order_walk_node(bst_node* node) {
  
  if (node == NULL){
  return;
  }
  
  else{
  bst_in_order_walk_node(node-&gt;left);
  print_node(node);
  bst_in_order_walk_node(node-&gt;right);
  }
  }
  
  // the same as bst_in_order_walk_node just that you do it for the whole tree.
  void bst_in_order_walk(bstree* bst) {
      if (bst != NULL) {
          bst_in_order_walk_node(bst-&gt;root);
  }
  }
  
  // deletes the all sub-tree of node
  void bst_free_subtree(bst_node* node) {
      if(node == NULL){
          return;
   }
   else{
      bst_free_subtree(node-&gt;left);
      bst_free_subtree(node-&gt;right);
      free(node-&gt;name);
      free(node);
   }
   }
  
   // Deletes the whole tree
   void bst_free_tree(bstree* bst) {
      if(bst != NULL &amp;&amp; bst-&gt;root != NULL) {
          bst_free_subtree(bst-&gt;root);
          bst-&gt;root = NULL;
  }
  }
  </code></pre>
  
  <p>Here's the the main and interface function:</p>
  
  <pre><code>// Kreiert eine Benutzeroberfläche
  bstree* interface(bstree *bst) {
      help();
      char *operation;
      unsigned long phone;
      char *name;
      read_line_context in;
      open_stdin(&amp;in);
      printf("&gt; ");
      while (read_line(&amp;in, &amp;operation, &amp;phone, &amp;name) == 0) {
          if (operation != NULL) {
              if (operation[0] == '?' &amp;&amp; phone &gt; 0) {
                  find_and_print(bst, phone);
              } else if (operation[0] == '+' &amp;&amp; phone &gt; 0 &amp;&amp; strlen(name) &gt; 0) {    
                  bst_insert_node(bst, phone, name);
              } else if (operation[0] == 'd') {
                  debug(bst);
              } else if (operation[0] == 'p') {
                  bst_in_order_walk(bst);
              } else if (operation[0] == 'q') {
                  break;
              } else {
                  printf("Inkorrekte Eingabe\n\n");
                  help();
              }
          }
          printf("&gt; ");
          phone = -1;
      }
      printf("Exiting...\n");
      close_file(&amp;in);
      return bst;
      }
  
  int main(int argc, char** argv) {
      // Create an empty search tree
      bstree bst;
      bst.root = NULL;
      bst.count = 0;
  
      if (argc != 2)
      {
          printf("Nutzung: %s &lt;Dateiname&gt;\n",argv[0]);
          return 1;
      }
  
      // reading the txt file
      read_file(argv[1], &amp;bst);
  
      // creating the interface
      interface(&amp;bst);
  
      bst_free_tree(&amp;bst);
  
      return 0;
      }
  </code></pre>
  
  <p>The program is supposed to take a text file which contains all of the phone numbers and the names and put it into a binary search tree. My program is compiling, but for some reason when I press "p" (pressing p should show you the "in-order" sorted phone numbers), no phone numbers show up and the program just waits for another input again.</p>