I am trying to sort an array of unsigned longs. However, for some reason the qsort is filling the entire array with zeroes rather than sorting it. I'll show my call to the function, comparator, and GDB.
This image shows the array before and after qsort, as well as my call to the function.
Here's the call:
qsort(scores[c], sizeof(scores[c]), sizeof(scores[c][0]), comparator);
Here is my comparator function called comparator:
int comparator(const void *p, const void *q)
{
if( *((unsigned long *)p) < *((unsigned long *)q)){
return -1;
}
else if( *((unsigned long *)p) == *((unsigned long *)q)){
return 0;
}
else{
return 1;
}
}
What causes this and what can I do about it?
The qsort
parameter sizeof(scores[c])
gives the size in bytes, not the number of elements to be sorted.
You probably get lots of out-of-range accesses that happens to pick up the zeros.