I have a struct that looks like this
typedef struct id_score{
int id;
int score;
} pair;
An array of size 50 holds pointers to these pairs
pair* array[50]
My comparator function looks like this
int struct_cmp(const void *a, const void* b) {
pair* ia = (pair*)a;
pair* ib = (pair*)b;
printf("ia's score: %d ib's score: %d??? \n", ia->score, ib->score);
return ib->score - ia->score;
}
My qsort function here
size_t arr_len = sizeof(array) / sizeof(pair);
qsort(array, arr_len, sizeof(pair), struct_cmp);
Now my problem is that in the struct_cmp function, my printf shows that the values that I think should be the count in each struct in the array are all interpreted as 0, thus not sorting the array at all. Outside of the function when I print through the array, the struct's have scores that it should have.
Any suggestions?? thank you!
ain a pointer to the element? The array elements are typepair*Sopair** ia = (pair**)a;?int, your function would be passed twoint *. Since you're sorting an array ofpair *, your function is passed twopair **.qsort(), considerqsort(array, arr_len, sizeof *array, struct_cmp);This is easier to code, review, and maintain thanqsort(..., ..., sizeof(pair), ...)