title: Linked list/Strings/Pointers in C
body: <p>I'm a newbie and trying to learn C, but faced some problem in implementing strings and pointer in a linked list. I was required to build a preorder tree using a linked list and one problem came out.</p>
  <p>BTW here are my codes:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  struct BinTreeNode {
      char *data ;
      struct BinTreeNode *left;
      struct BinTreeNode *right;
  };
  
  struct BinTreeNode *initBinTreeNode(char *data);
  
  void visit(char *data);
  
  void preorder(struct BinTreeNode *node);
  void postorder(struct BinTreeNode *node);
  void inorder(struct BinTreeNode *node);
  
  int main(void)
  {
      struct BinTreeNode *root, *parent1, *parent2;
    /* growing the tree */
      root = initBinTreeNode(&quot;5&quot;);
      root-&gt;left  = initBinTreeNode(&quot;Mee (F) &amp; Keong (M)&quot;);
      root-&gt;right = initBinTreeNode(&quot;Saw (F)&quot;);
      parent1 = root-&gt;left;
      parent1-&gt;left  = initBinTreeNode(&quot;Min (F) &amp; Heng (M)&quot;);
      parent1-&gt;right = initBinTreeNode(&quot;Ning (F)&quot;);
      parent2 = parent1-&gt;left;
      parent2-&gt;left  = initBinTreeNode(&quot;Rui (F)&quot;);
    /* traverse and print tree */
      printf(&quot;\nPreorder traversal:\t&quot;);  preorder(root);
      
      return(0);
  }
  
  void preorder(struct BinTreeNode *node)
  {
      if (node){  /* if Node exists */
          visit(node-&gt;data);
          preorder(node-&gt;left);
          preorder(node-&gt;right);
      }
  }
  
  void visit(char *data)
  {
      printf(&quot;%c -&quot;, *data);
  }
  
  struct BinTreeNode *initBinTreeNode(char *data)
  {
      struct BinTreeNode *temp;
      temp = malloc(sizeof(struct BinTreeNode));
      if (temp == NULL)
      {
          printf (&quot;Memory allocation failed.&quot;);
          return NULL;
      }
  
      (*temp).data = data;
      (*temp).left = NULL;  
      (*temp).right = NULL;   
      return (temp);          
  }
  </code></pre>
  <p>When I have {&quot;5&quot;},{&quot;Mee (F) &amp; Keong (M)&quot;},{&quot;Saw (F)&quot;},{&quot;Min (F) &amp; Heng (M)&quot;},{&quot;Ning (F)&quot;},{&quot;Rui (F)&quot;} in my list, it comes out only the first word is printed out like this:</p>
  <pre><code>Preorder traversal:     5 -M -M -R -N -S -
  </code></pre>
  <p>The preorder output that I want, should be:</p>
  <pre><code>Preorder traversal:     5 -Mee (F) &amp; Keong (M) -Min (F) &amp; Heng (M) -Rui (F) -Ning (F) -Saw (F) -
  </code></pre>
  <p>May I know, what I am missing? I would be really grateful for any help and advice!</p>
  <p><img src="https://i.stack.imgur.com/G2dv0.jpg" alt="Illustration of Preorder tree" /></p>