title: Shuffle array from text file (seg fault)
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  
  // Accepts: command line input
  // Returns: 0 if no error
  
  int main(int argc, char *argv[] )
  {
  int x = 0, i, lCount = 0, maxLen = 0;
      char line[500], *temp;
      FILE *file = fopen("names.txt", "r" );
  //  check if file exists
      if (file == NULL){
          printf("Cannot open file\n");
          return 1;
      }
  //  Gets lines, max length of string    
  while (fgets(line, sizeof(line), file) != NULL){
          lCount++;
      if (strlen(line) &gt; maxLen)
          maxLen = strlen(line);
      }
      rewind(file);
      char *lArray[lCount];
      while (fgets(line, sizeof(line), file) != NULL) {
          lArray[x] = malloc(strlen(line));
          if (lArray[x] == NULL){
              printf("A memory error occurred.\n");
              return(1);
          }
              strcpy(lArray[x], line);
  //  change \n to \0
      lArray[x][strlen(lArray[x])-1] = '\0';
      x++;
      }
      printf("File %s has %d lines with maximum length of %d characters\n",
          argv[1], lCount, maxLen);
      printf("Original Array\n");
      for (x = 0; x &lt; lCount; x++)
          printf("%2d %s\n", x, lArray[x]);
  //  Shuffle array
      srand( (unsigned int) time(NULL));
      for (x = lCount - 1; x &gt;= 0; x--){
          i = (int) rand() % lCount;
          temp = lArray[x];
          lArray[x] = lArray[i];
          lArray[i] = temp;
      }
      printf("\nShuffled Array\n");
      for (x = 0; x &lt; lCount; x++)
          printf("%2d %s\n", x, lArray[x]);
  //  free allocated memory
      for (x = 0; x &lt; lCount; x++)
          free(lArray[x]);
      free(lArray);
      fclose(file);
      return 0;
  }
  </code></pre>
  
  <p>For the most part I have all the code written out. Only 2 problems, a segmentation fault at the end of the output and I want to change it around a bit to make it cleaner. any suggestions? Thanks!
  It should also have this structure:</p>
  
  <pre><code>/* Read the file into an array */
  /* Count lines and maximum length */
  /* Compute maximum width for array */
  /* Get file pointer to the beginning */
  /* Reserve memory for a dynamic array of strings */
  /* Read a line and store in allocated memory */
  /* Turn the \n into \0 */
  /* Print lines from array (test) */
  /* Shuffle array */
  /* Print lines from array (test) */
  /* Free memory and close file */
  </code></pre>