title: Recursive function passes NULL pointer when the argument is not NULL
body: <p>The folowing program is intended to store words alphabetically in the Binary Search Tree using the strcmp function. The issue, detailed under the program, is that no pointer is passed in the recursive call of the function in the last part of the function.</p>
  
  <pre><code>typedef struct NodT{
     char word[30];
     struct NodT *left, *right;
  } NOD;
  
  void reset_field(NOD *nod){
      int i;
      for(i=0; i&lt;30; i++){
          nod-&gt;word[i]='\0';
      }
  }
  
  void enter_recursively(NOD *nod, char *word){
      if(nod==NULL){
          nod= (NOD *) malloc(sizeof(NOD));
         nod-&gt;left=NULL;
         nod-&gt;right=NULL;
         reset_field(nod);
         strcpy(nod-&gt;word, word);
         return;
     }
  
     if(nod-&gt;word[0]=='\0'){
         strcpy(nod-&gt;word, word);
         return;
     }
  
     if(strcmp(nod-&gt;word, word)==0) return;
  
     if(strcmp(nod-&gt;word, word)&lt;0){
         enter_recursively(nod-&gt;right, word);//the problem seems to be here
         printf("right\n");
     }
     else{
         enter_recursively(nod-&gt;left, word);//...and here
         printf("left\n");
     }
     //the NULL pointer is being sent over, which is peculiar
  }
  </code></pre>
  
  <p>The thing is that, when I pass the pointers(left, right) from the structure to the recursive function in the if-else conditions, it takes a NULL value on the other side, which it shouldn't do because they are not NULL after alocating the first word in the root and the second in the right or left depending on strcmp, alocation when malloc is used to create the new storage space for the word.</p>
  
  <p>UPDATE: The new script using double pointers:</p>
  
  <pre><code>typedef struct NodT{
      int key;
      char word[30];
      struct NodT *left, *right;
  } NOD;
  
  void enter_recursively(NOD **nod, char *word){
          printf("N: %p\n", nod);
      printf("NL: %p\n", (**nod).left);
      printf("NR: %p\n", (**nod).right);
          if(nod==NULL){
              nod=malloc(sizeof(NOD));        
              (**nod).left=NULL;
              (**nod).right=NULL;
              strcpy((**nod).word, word);
              return;
          }
          if((**nod).word[0]=='\0'){
              strcpy((**nod).word, word);
              return;
          }
  
      if(strcmp((**nod).word, word)==0) return;
  
          if(strcmp((**nod).word, word)&lt;0){
              enter_recursively((**nod).right, word);
          }
          else{
              enter_recursively((**nod).left, word);
          }
  </code></pre>
  
  <p>I get segmentation fault and I don't know why.</p>