title: How to qsort an array of pointers to char in C?
body: <p>Suppose I have an array of pointers to char in C:</p>
  
  <pre><code>char *data[5] = { "boda", "cydo", "washington", "dc", "obama" };
  </code></pre>
  
  <p>And I wish to sort this array using qsort:</p>
  
  <pre><code>qsort(data, 5, sizeof(char *), compare_function);
  </code></pre>
  
  <p>I am unable to come up with the compare function. For some reason this doesn't work:</p>
  
  <pre><code>int compare_function(const void *name1, const void *name2)
  {
      const char *name1_ = (const char *)name1;
      const char *name2_ = (const char *)name2;
      return strcmp(name1_, name2_);
  }
  </code></pre>
  
  <p>I did a lot of searching and found that I had to use <code>**</code> inside of qsort:</p>
  
  <pre><code>int compare_function(const void *name1, const void *name2)
  {
      const char *name1_ = *(const char **)name1;
      const char *name2_ = *(const char **)name2;
      return strcmp(name1_, name2_);
  }
  </code></pre>
  
  <p>And this works.</p>
  
  <p>Can anyone explain the use of <code>*(const char **)name1</code> in this function? I don't understand it at all. Why the double pointer? Why didn't my original function work?</p>
  
  <p>Thanks, Boda Cydo.</p>