title: Accessing a struct causes segmentation fault
body: <p>Why am I getting a segmentation fault when I go to access the struct <code>vm</code>? The code is as follows:</p>
  
  <pre><code>BOOLEAN vm_init(struct vm * vm)
  {
      struct vm_node * vmNode;
      vmNode = malloc(sizeof(struct vm_node));
      vm-&gt;item_list-&gt;head = vmNode;
      vm-&gt;coinsfile = " ";
      vm-&gt;foodfile = " ";
      return FALSE;
  }
  
  /* 
   * Loads data from the .dat files into memory. 
   * */
  BOOLEAN load_data(struct vm * vm, const char * item_fname, 
                    const char * coins_fname) {
      FILE *file;
      file = fopen(item_fname, "r+");
      char buf[256]={};
      struct vm_node *vmNode;
      vmNode = malloc(sizeof(struct vm_node));
      vmNode-&gt;next = NULL;
  
      while (fgets(buf, sizeof buf, file) != NULL) {
          addNodeBottom(buf,vmNode);
      }
  
      /* Test reason for reaching NULL. */
      if (feof(file)) /* if failure caused by end-of-file condition */
      {
      }
      else if (ferror(file)) /* if failure caused by some other error      */
      {
          perror("fgets()");
          fprintf(stderr, "fgets() failed in file %s at line # %d\n", __FILE__,
                  __LINE__ - 9);
          exit(EXIT_FAILURE);
      }
      fclose(file);
  </code></pre>
  
  <p>If I try to access <code>vm-&gt;item_list-&gt;head</code> it segfaults. <code>item_list</code> is the container to a linked list, which is what <code>vmNode</code> is. So i need to store <code>vmNode</code> within <code>vm-&gt;item_list-&gt;head</code>.</p>
  
  <p>But if I do the following:</p>
  
  <pre><code>vm-&gt;item_list-&gt;head = vmNode; //it segfaults... 
  </code></pre>
  
  <p>Any clues? </p>
  
  <p>The typedefs for vm and vm_node are as follows.</p>
  
  <pre><code>struct stock_item
  {
      char id[IDLEN+1];
      char name[NAMELEN+1];
      char description[DESCLEN+1];
      struct price price;
      unsigned on_hand;
  };
  
  /* The data structure that holds a pointer to the stock_item data and a
   * pointer to the next node in the list
   */
  struct vm_node
  {
      struct stock_item * data;
      struct vm_node * next;
  };
  
  /* The head of the list - has a pointer to the rest of the list and a 
   * stores the length of the list 
   */
  struct vm_list
  {
      struct vm_node * head;
      unsigned length;
  };
  
  /* This is the head of our overall data structure. We have a pointer to 
   * the vending machine list as well as an array of coins. 
   */
  struct vm
  {
      struct vm_list * item_list;
      struct coin coins[NUMDENOMS];
      char * foodfile;
      char * coinsfile;
  };
  </code></pre>