title: How to reuse a *char[] in a loop?
body: <p>Writing POSIX C (or at least trying to) to run on a typical modern desktop computer.</p>
  <p>I've tried searching SO and Google but I don't think I know enough to get the correct search terms.</p>
  <p>My program does the following:</p>
  <ul>
  <li>Reads lines from a text file.</li>
  <li>Line is added to a <code>*char[1000]</code> called <code>hay</code> (as in, haystack).</li>
  <li>Once 1000 lines have been added to <code>hay</code>, <code>hay</code> is passed to a pthread.</li>
  </ul>
  <p>The bit I can't figure out is:</p>
  <ul>
  <li>How do I &quot;reinitialise&quot; <code>hay</code> so that it points to a new <code>char[1000]</code>?</li>
  </ul>
  <p>This is because I want my main thread to keeping reading lines and dispatch the next 1000 to another thread.</p>
  <p>Currently I have this:</p>
  <pre><code>#define ITEMS_PER_THREAD 1000
  // &lt;SNIP&gt;
  char *hay[ITEMS_PER_THREAD];
  for (int i=0; i&lt;ITEMS_PER_THREAD; i++) {
      hay[i] = malloc(256);
      if (hay[i] == NULL) {
          fprintf(stderr, &quot;[E] Couldn't allocate memory for haystack item!\n&quot;);
          return 0;
      }
  }
  // &lt;SNIP some definitions...&gt;
  size_t hay_count = 0;
  while((read = getline(&amp;line, &amp;len, f)) != -1) {
      strcpy(hay[hay_count], line);
      hay_count++;
      if (hay_count == ITEMS_PER_THREAD) {
          // Start worker thread
          sem_wait(&amp;semWorkers);
          pthread_t new_pt = dispatch(hay, hay_count); // function to create the thread
          pthread_join(new_pt, NULL);
          hay_count = 0; // reset the counter
          // What goes here?? How do I get a &quot;new&quot; *hay[ITEMS_PER_THREAD]??
      }
  }
  </code></pre>