title: Using stdlib's qsort() to sort an array of strings
body: <p>Some preface:  I'm a computer engineering student taking a first class in C after 3 semesters of Java (up to data structures).  This question is in relation to a homework assignment, but a few steps removed from solving it for me.</p>
  
  <p>I have an input file that I read into memory such that it is stored in char[9][500].  I read in at most 500 strings of maximum length 8.  I am attempting to sort this array using stdlib's built in qsort() function, and am having some memory errors.</p>
  
  <p>Important snippets of code:</p>
  
  <pre><code>char data[4][500][60];
  char debug[500][9];
  size_t count = 0;
  
  /* initialize file, open for reading */
  FILE* pUserlog;
  pUserlog = fopen("userlog","r");
  
  while(!feof(pUserlog))
  {
      fscanf(pUserlog, "%9s %8s %16s",debug[count], data[1][count], data[2][count]);
      fgets(data[3][count], 60, pUserlog);
      count++;
  }
  </code></pre>
  
  <p>This section reads the data into the arrays.  The array of interest in this part is "debug".  This is the array specified above.  Here is my comparison function for qsort:</p>
  
  <pre><code>int compare(const void* a, const void* b)
  {
      const char **ia = (const char **)a;
      const char **ib = (const char **)b;
      puts("I'm in compare!");
      return strncmp(*ia, *ib,8);
  }
  </code></pre>
  
  <p>This is my attempt to call qsort:</p>
  
  <pre><code>size_t debug_len = sizeof(debug)/sizeof(char*);
  printf("debug len: %d, count: %d, sizeof(char*): %d\n",debug_len,count,sizeof(char*));
  qsort(debug,count, sizeof(char *), compare);
  </code></pre>
  
  <p>I attempted substituting debug_len in my call where count is, but I am still segfaulting.  Here is the output:</p>
  
  <pre>
  $ ./test
  debug len: 1125, count: 453, sizeof(char*): 4
  I'm in compare!
  Segmentation fault (core dumped)
  </pre>
  
  <p>Thank you!</p>