title: When utilizing qsort() on a tree made of structs, I am not receiving a sorted array back
body: <p>Using the <code>qsort</code> on a tree made of structs, I am not getting a sorted array back.</p>
  
  <p>I've tried manipulating the comparator function and <code>qsort</code> but not sure what the problem is.</p>
  
  <pre><code>typedef struct nodeBST { // struct
      char *key;
      int count;
      struct nodeBST *left;
      struct nodeBST *right;
  } nodeBST;
  </code></pre>
  
  <hr>
  
  <pre><code>qsort(*words, numTokensActual, sizeof(nodeBST), comparator); //qsort
  </code></pre>
  
  <hr>
  
  <pre><code>for (i = 0; i &lt; numTokensActual; i++) {
      printf("sorted words[%d]:%s: %d \n", i,
             ((struct nodeBST *)words[i])-&gt;key,
             ((struct nodeBST *)words[i])-&gt;count); //traverse to print
  }
  </code></pre>
  
  <hr>
  
  <pre><code>struct nodeBST *words[4]; //creation of array and malloc for space
  
  int z;
  
  for (z = 0; z &lt; 4; z++) {
      words[z] = malloc(sizeof(nodeBST));
  }
  </code></pre>
  
  <hr>
  
  <pre><code>int comparator(const void *p, const void *q) { //compare function
      struct nodeBST *a = (struct nodeBST **)p;    
      struct nodeBST *b = (struct nodeBST **)q;
  
      return a-&gt;count - b-&gt;count;
  } 
  </code></pre>
  
  <hr>
  
  <pre><code>printf("%d \n"((struct nodeBST*)words[i])-&gt;count); //print output
  </code></pre>
  
  <hr>
  
  <pre><code>int numTokensActual = AddToArray(root, words, 0);
  </code></pre>
  
  <hr>
  
  <pre><code>int AddToArray(nodeBST *node,  nodeBST **arr, int i) {
      if (node == NULL)
          return i;
  
      if (node-&gt;left != NULL)
          i = AddToArray(node-&gt;left, arr, i);
  
      //printf("Adding To Array[%d]: %s:%d\n",i, node-&gt;key, node-&gt;count);
      //arr[i] = node;
      arr[i] = newNodeBST2(node-&gt;key, node-&gt;count);
      //printf("added array[%d]: %s\n", i, arr[i]-&gt;key);
      i++;
      if (node-&gt;right != NULL)
          i = AddToArray(node-&gt;right, arr, i);
  
      return i;
  }
  </code></pre>
  
  <p>I expect the output to give me a sorted array but the output is:</p>
  
  <pre><code>0 
  0 
  49 
  6
  </code></pre>