title: Values stored in struct changing changing for no reason that I can think of
body: <p>This program is a guessing game that gets better as you play it(I've not included some of the methods here). Its basically a binary tree made of of structs. Only the structs at the end of the tree are guesses. All other structs are questions. Every single time I play the game, it starts off fine. However, the third time I play the game it changes the questions of all the structs within the tree change to 1 of the questions and I cant understand why(The overall structure of the tree seems to be fine because it ends at the correct guesses). I code in a really messy manner and I apologize for that. However any help on how I can fix this would be really appreciated.</p>
  
  <pre><code>struct node
  {
      char *guess;
      char *question;
      struct node *yes_ptr;
      struct node *no_ptr;
  };
  struct node *createObjectNode(char *guess)
  {
     struct node *newNode = malloc(sizeof(struct node));
     newNode-&gt;guess = guess;
     return newNode;
  }
  
  int response(char *str)
  {
    char yesOrNo[5];
    printf("%s\n&gt;", str);
    fgets(yesOrNo, 5, stdin);
    int i = strlen(yesOrNo)-1;
    yesOrNo[i] = '\0';
    if(strcmp(yesOrNo, "yes") == 0)
    {
      return 1;
    }
    if(strcmp(yesOrNo, "no") == 0)
    {
      return 0;
    }
    return 0;
  }
  int main(int argc, char *argv[])
  {
  
    struct node *root;
    char guess[20];
    char question[120];
    char buffer[120];
    root = createObjectNode("pangolin");
    if(response("Shall we play a game?") == 1)
    {
      snprintf(buffer, sizeof(buffer), "Is it a %s?", getGuess(root));
      if(response(buffer) == 1)
      {
        printf("I win\n");
      }
      else
      {
        printf("What were you thinking of?\n&gt;");
        fgets(guess, 50, stdin);
        int i = strlen(guess)-1;
        if( guess[ i ] == '\n') 
          guess[i] = '\0';
        printf("Please give me a question about a %s,so I can tell the difference between a %s and a ", 
        guess, guess);
        printf("%s\n&gt;",getGuess(root));
        fgets(question, 50, stdin);
        i = strlen(question)-1;
        if( question[ i ] == '\n') 
          question[i] = '\0';
        snprintf(buffer, sizeof(buffer), "What is the answer for %s?", guess);
        if(response(buffer) == 1)
        {
          struct node *child1 = createObjectNode(guess);
          struct node *child2 = createObjectNode(getGuess(root));
          child1-&gt;yes_ptr = NULL;
          child2-&gt;no_ptr = NULL;
          child1-&gt;question = NULL;
          child2-&gt;question = NULL;
          root-&gt;guess = NULL;
          root-&gt;question = question;
          root-&gt;yes_ptr = child1;
          root-&gt;no_ptr = child2;      
        }
        else
        {
          struct node *child1 = createObjectNode(guess);
          struct node *child2 = createObjectNode(getGuess(root));
          child1-&gt;yes_ptr = NULL;
          child2-&gt;no_ptr = NULL;
          child1-&gt;question = NULL;
          child2-&gt;question = NULL;
          root-&gt;guess = NULL;
          root-&gt;question = question;
          root-&gt;yes_ptr = child2;
          root-&gt;no_ptr = child1;  
        }
        printTree(root);
  
      }
    }
  
    while(1)
    {
      char guess2[20];
      char question2[120];
      char buffer2[120];
      struct node *thisNode = malloc(sizeof(struct node));
      thisNode-&gt;question = root-&gt;question;
      thisNode-&gt;yes_ptr = root-&gt;yes_ptr;
      thisNode-&gt;no_ptr = root-&gt;no_ptr;
      if(response("Shall we play again?"))
      {
        while(thisNode-&gt;yes_ptr != NULL &amp;&amp; thisNode-&gt;no_ptr != NULL &amp;&amp; !thisNode-&gt;guess)
        {
          if(response(thisNode-&gt;question) == 1)
          {
            thisNode = thisNode-&gt;yes_ptr;
          }
          else
          {
            thisNode = thisNode-&gt;no_ptr;
          }
        }
        snprintf(buffer2, sizeof(buffer2), "Is it a %s?", getGuess(thisNode));
        if(response(buffer2) == 1)
        {
          printf("I win\n");
          break;
        }
        else
        {
          printf("What were you thinking of?\n&gt;");
          fgets(guess2, 10, stdin);
          printf("Please give me a question about a %s,so I can tell the difference between a %s and a %s\n&gt;", 
                  guess2, guess2, getGuess(thisNode));
          int i = strlen(guess2)-1;
          guess2[i] = '\0';
          fgets(question2, 120, stdin);
          i = strlen(question2)-1;
          if( question2[i] == '\n') 
            question2[i] = '\0';
          snprintf(buffer2, sizeof(buffer2), "What is the answer for %s?", guess2);
          if(response(buffer2) == 1)
          {
            struct node *child1 = createObjectNode(guess2);
            struct node *child2 = createObjectNode(getGuess(thisNode));
            child1-&gt;yes_ptr = NULL;
            child2-&gt;no_ptr = NULL;
            child1-&gt;question = NULL;
            child2-&gt;question = NULL;
            thisNode-&gt;guess = NULL;
            thisNode-&gt;question = question2;
            thisNode-&gt;yes_ptr = child1;
            thisNode-&gt;no_ptr = child2;  
          }
          else
          {
            struct node *child1 = createObjectNode(guess2);
            struct node *child2 = createObjectNode(getGuess(thisNode));
            child1-&gt;yes_ptr = NULL;
            child2-&gt;no_ptr = NULL;
            child1-&gt;question = NULL;
            child2-&gt;question = NULL;
            thisNode-&gt;guess = NULL;
            thisNode-&gt;question = question2;
            thisNode-&gt;yes_ptr = child2;
            thisNode-&gt;no_ptr = child1;  
          }
        }
        printTree(root);
  
      } 
  
    } 
    return 0;
  }  
  </code></pre>