title: Cons Cell data structure in C
body: <p>I'm a newbie at C, in the early stages of building a small Scheme interpreter. For this part of the project I'm trying to build a simple cons cell data structure. It should take a list like</p>
  
  <p><code>(a b c)</code> </p>
  
  <p>and represent it internally like so:</p>
  
  <pre><code>[ ][ ] -&gt; [ ][ ] -&gt; [ ][/]
   |         |         |
   A         B         C 
  </code></pre>
  
  <p>To test that it's working correctly, I have a print function to to echo out the input. Here is the code that isn't working:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include "lexer.h"
  #include "parse.h"
  
  char token[20]; 
  
  
  struct conscell {
      char *data;
      struct conscell *first, *rest;
  };
  
  void S_Expression ()
  {   
      /* function from lexer to receive input a split into tokens no greater than 20 */
      startTokens(20);
  
      /* gets the next token */
      strcpy(token, getToken());
  
      /* List is a typedef for the struct conscell */
      List tree = createList ();
      tree = nextNode (tree);
      printList(tree);
  
  }
  
  
  List createList ()
  {
      List node = malloc(sizeof (List));
  
      if (node == NULL) {
          printf("Out of memory!\n");
          exit(1);
      }
      node-&gt;data = NULL;
      node-&gt;first = NULL;
      node-&gt;rest = NULL;
  
      return node;
  }
  
  /* Recursive function to build cons cell structure */
  List nextNode (List node)
  {
      node = createList ();
  
      if (token[0] == '(')
      {         
         strcpy(token, getToken());
         node-&gt;first = nextNode(node-&gt;first);
         node-&gt;rest = nextNode(node-&gt;rest);         
       }
  
      else
      {
         if (token[0] == ')')
         {
            node = NULL;
         }
  
         else
         {
             List temp = createList();
             temp-&gt;data = token;
             temp-&gt;first = NULL;
             temp-&gt;rest = NULL;
  
             node-&gt;first = temp;
  
             strcpy(token, getToken());
             node-&gt;rest = nextNode(node-&gt;rest);            
         }
     }
     return node;
  }
  
  /* Prints output. So far, just trying to print symbols */
  void printList(List node)
  {
      if (node != NULL)
      {
        if (node-&gt;data != NULL)
        {        
          printf("%s", node-&gt;data);
  
        }
      }
  }
  </code></pre>
  
  <p>So far can't print out anything. I'm almost positive its a pointer issue. If anyone could point me (no pun intended) in the right direction, it'd be very much appreciated.</p>
  
  <p>Thank you</p>