title: string_comparator in C
body: <p>Okay so I need to several quite long strings in C. So I say to myself "why, you'd better use that handy dandy qsort function! Better write yourself a string_comparator for it!"</p>
  
  <p>So of course I do and here she is:</p>
  
  <pre><code>int string_comparator(const void* el1, const void* el2) {
  
  char* x = (char*) el1;
  char* y = (char*) el2;
  
  int str_len = strlen(x);
  int i = 0;
  for (; i &lt; str_len; i++) {
  
      //when there are non-equal chars
      if (x[i] != y[i]) {
          break;
      }
  }
  
  return x[i] - y[i];
  }
  </code></pre>
  
  <p>So of course I pass my handy dandy string_comparator function to the C qsort function as such:</p>
  
  <pre><code>qsort(list.words, list.num_words, sizeof(char*), string_comparator);
  </code></pre>
  
  <p>list is a struct that holds a char** (words) and ints which refer to the number of words held by it (such as num_words)</p>
  
  <p>Now I have the problem where my list is not getting sorted alphabetically like I had hoped! I put a bunch of printf statements in my comparator and it printed out garbage values for the strings every time so I'm fairly sure that is the problem. But why is that the problem?? I've used qsort before (never to sort words..just sorting characters) and from what I understand this should work...What's going wrong here?</p>
  
  <p>I appreciate any suggestions!</p>