title: How to qsort an array of pointers that use structs?
body: <p>I want to sort an array of pointers by Id's. However qsort fails to work because of my lack of experience with pointers.</p>
  
  <pre><code>typedef struct block{
      int Id;
      char * name;
  } block;
  
  typedef struct {
      block ** data;
      int size_array;
  } database;
  
  if( ( database = malloc(sizeof(database)) ) == NULL ) {
      printf("Error: malloc failed\n");
      exit(EXIT_FAILURE);
  }
  
  if( ( database-&gt;data = malloc( sizeof( block * ) * database-&gt;size_array ) ) == NULL ) {
      exit(EXIT_FAILURE);
  }
  
  for( i = 0; i &lt; database-&gt;size_array; i++ ) {
      DB-&gt;data[i] = NULL;
  }
  </code></pre>
  
  <p>I'm trying to sort using qsort, but I'm clearly doing something wrong.</p>
  
  <pre><code>int compare(const void * a, const void * b ){
      const block * eval1 = a;
      const block * eval2 = b;
  
      if (eval1-&gt;Id &lt; eval2-&gt;Id){
          return(-1);
      }
  
      else if (eval1-&gt;Id &gt; eval2-&gt;Id)
          return 1;
  
      return 0;
  }
  </code></pre>
  
  <p>Calling code:</p>
  
  <pre><code>    qsort(database-&gt;data, database-&gt;size_array, sizeof(int), compare);
      for(i = 0; i&lt;DB-&gt;database-&gt;size_array; i++) {
          if(database-&gt;data[i] != NULL)
              printf("Id: %d\n", database-&gt;data[i]-&gt;Id);
  
      }
  </code></pre>
  
  <p>By using a for loop I see that the sorting failed.</p>
  
  <p>Insert Function:</p>
  
  <pre><code>void insertE(int Id, char * name){
      block * item = malloc(sizeof(block));
      item-&gt;name = malloc(strlen(name)+1);
      strcpy(item-&gt;name, name);
      item-&gt;Id = Id;
  }
  </code></pre>
  
  <p>current output:</p>
  
  <pre><code>Id: 13
  Id: 243
  Id: 121
  Id: 87
  .
  .
  .
  </code></pre>