title: C - segmentation fault using struct member values
body: <p>I'm running head-long into a segmentation fault that I'm not sure of the reason behind.  </p>
  
  <p>Short story... I store file names into members of a struct, then use those members to open files to load their data into linked lists.  This is working fine when I only have two file, but when I go to add a third, I get a segmentation fault opening the first file.</p>
  
  <p>Code will hopefully illustrate better...</p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      /* Initalise tennisStore struct  */
      TennisStoreType *ts;
      systemInit(ts);
  
      /* Variables */
      ts-&gt;stockFile = "stock.csv";
      ts-&gt;custFile = "customer.csv";
      ts-&gt;salesFile = "sales.csv";
  
       /* Load data from files */
       loadData(ts, ts-&gt;custFile, ts-&gt;stockFile);
  
  ...
  }
  </code></pre>
  
  <p>The struct details for ts...</p>
  
  <pre><code>typedef struct tennisStore
  {
     CustomerNodePtr headCust;  
     unsigned customerCount;
     StockNodePtr headStock;
     unsigned stockCount;
     char *custFile;
     char *stockFile;
     char *salesFile;
  } TennisStoreType;
  </code></pre>
  
  <p>systemInit() seems pretty innocuous, but here's the code just in case...</p>
  
  <pre><code>void systemInit(TennisStoreType *ts)
  {
      /* Set ts options to be ready */
      ts-&gt;headCust = NULL;
      ts-&gt;headStock = NULL;
      ts-&gt;customerCount = 0;
      ts-&gt;stockCount = 0;
  }
  </code></pre>
  
  <p>loadData()...</p>
  
  <pre><code>void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
  {
      /* Load customer data */
      addCustNode(ts, customerFile);
  
      /* Load stock data */
      addStockNode(ts, stockFile);
  }
  </code></pre>
  
  <p>Here's where the problem occurs...</p>
  
  <pre><code>void addStockNode(TennisStoreType* ts, char* stockFile)
  {
      /* Variables */
      StockNodePtr head, new, current, previous;
      unsigned stkLevel;
      char *stkTok1, *stkTok2, *stkTok3, *stkTok4;
      char buf[BUFSIZ];
      float stkPrice;
      FILE *stream;
  
      /* Set head */
      head = NULL;
  
      /* Open stock file  */
      stream = fopen(stockFile, "r");     &lt;-- segmentation fault when sales.csv line   included
      assert(stream);
  
      while (fgets(buf, BUFSIZ, stream))
      {
          ...
      }
  ...
  }
  </code></pre>
  
  <p>As above, when the <em>ts->salesFile = "sales.csv"</em> line is included in main, the segmentation fault occurs.  When it isn't, all is fine (file opens, I can read from it, write to it etc).  Cannot for the life of me understand why, so I'm appealing to your good nature and superior knowledge of C for potential causes of this problem.</p>
  
  <p>Thanks!</p>