title: Issue with qsort() - The sorting is not properly done (C)
body: <p>I have a question regarding qsort.</p>
  
  <p>This is a little bit weird, but my qsort function does not give me the correct output. The strange thing is that some of my compare functions are identical to my past projects but they don't give me the correct input at all. I am not sure how to test it.</p>
  
  <p>For example:</p>
  
  <pre><code>int comp_name_asc(const void *a, const void *b)
  {
    const Rec *prec1 = (const Rec *) a;
    const Rec *prec2 = (const Rec *) b;
    return strcmp(prec1-&gt;name, prec2-&gt;name); 
  }
  
  int comp_name_desc(const void *a, const void *b)
  {
    const Rec *prec1 = (const Rec *) a;
    const Rec *prec2 = (const Rec *) b;
    return strcmp(prec2-&gt;name, prec1-&gt;name);
  }
  </code></pre>
  
  <p>The second function should be descending order, but the result is identical: it's always in ascending order. I have checked to make sure the right function is entered at the right time. Rec is a typedef for a struct I made, which has a char * name parameter.</p>
  
  <p>Also (MODIFIED to avoid overflow):</p>
  
  <pre><code>int comp_size_asc(const void *a, const void *b)
  {
    const Rec *prec1 = (const Rec *) a;
    const Rec *prec2 = (const Rec *) b;
  
    if (prec1-&gt;byteSize &gt; prec2-&gt;byteSize)
      return 1;
    else if (prec1-&gt;byteSize &lt; prec2-&gt;byteSize)
      return -1;
    else
      return 0;
  }
  </code></pre>
  
  <p>The result is completely weird, not ascending or descending (ie: 500, 515, 100, 200...).
  byteSize is of type off_t obtained by doing:</p>
  
  <pre><code>char *path; // Build the path
  struct stat sb;
  if (lstat(path, &amp;sb) == 0) {
    // Read sb.st_size
  </code></pre>
  
  <p>I am really not sure how to debug this. All I know, is that the appropriate compare function is entered, and that some similar compare functions used to work in the past.</p>
  
  <p>Any ideas or how I can debug this would be welcome. Thank you.</p>
  
  <p>EDIT:</p>
  
  <p>Adding the call to qsort:</p>
  
  <pre><code>int index = 0;
  Rec **array = (Rec **) malloc(sizeof(Rec *) * capacity);
  // Adds element to the array...
  qsort(array, index, sizeof(Rec *), comp_name_desc);
  </code></pre>
  
  <p>(Every time an element is added to the array, index is incremented.)</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>The solution was given below. Thank you!</p>
  
  <p>I had to change:</p>
  
  <pre><code>const Rec *prec1 = (const Rec *) a;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>const Rec *prec1 = *(const Rec **) a;
  </code></pre>
  
  <p>because of how I defined my array. Thanks!</p>