c - Sorting Array of Struct Pointers -


i trying sort array of structs using qsort() frustratingly, it's not working. have read manpage qsort() , think have comparator function syntactically looks okay, when print "sorted" array after calling qsort(), nothing sorted in array.

the code:

#include <stdlib.h> #include <stdio.h>  #define array_sz 5  typedef struct singlechar {     unsigned char character;     unsigned int weight; } *singlecharptr;  int compareweights(const void *a, const void *b) {     const singlecharptr p1 = (singlecharptr)a;     const singlecharptr p2 = (singlecharptr)b;      // printf("weight1: %u\tweight2: %u\n", p1->weight, p2->weight);     // return (p1->weight - p2->weight);      if (p1->weight < p2->weight)         return -1;     else if (p1->weight > p2->weight)         return 1;     else         return 0; }   singlecharptr makechar(unsigned char c, unsigned int w) {     singlecharptr scptr = malloc(sizeof(struct singlechar));      if (!scptr)     {         fprintf(stderr, "[error] out of memory\n");         exit(1);     }      scptr->character = c;     scptr->weight = w;      return scptr; }  int main(void) {     singlecharptr *chars = malloc(array_sz * sizeof(singlecharptr));      chars[0] = makechar('b', 3);     chars[1] = makechar('e', 7);     chars[2] = makechar('a', 4);     chars[3] = makechar('d', 6);     chars[4] = makechar('c', 2);      qsort(chars, array_sz, sizeof(singlecharptr), &compareweights);      int i;     (i = 0; < array_sz; i++)     {         printf("character: %c\tweight: %u\n", chars[i]->character, chars[i]->weight);         free(chars[i]);     }      free(chars);      return 0; } 

also, in comparator function (compareweights()), found out when print weight of structs pointed singlecharptr, 0s of them.

any pointer right direction highly appreciated.

the problem: qsort() passes in pointers elements compared comparator function, , not elements themselves. so, arguments compareweights() function const singlecharptr *, disguised const void *. should in function is:

const singlecharptr p1 = *(const singlecharptr *)a; 

etc.


sidenotes:

i. if assumption had been valid, wouldn't have needed cast:

const singlecharptr p1 = a; 

is preferred over

const singlecharptr p1 = (singlecharptr)a; 

because of this.

ii. comparison function need not return -1, 0 or 1. should return integer less 0, 0 or greater 0. thus, huge if in compareweight() superfluous, write

return p1->weight - p2->weight; 

instead.

iii. singlecharptr *chars = malloc(array_sz * sizeof(singlecharptr)); - why? use chars array locally in main() function, don't need dynamic allocation that. why not write

singlecharptr chars[array_sz]; 

instead?


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -