Hello,
I have a question regarding qsort.
This is a little bit weird, but my qsort function does not give me the correct output. The strange thing is that some of my compare functions are identical to my past projects but they don't give me the correct input at all. I am not sure how to test it.
For example:
int comp_name_asc(const void *a, const void *b)
{
const Rec *prec1 = (const Rec *) a;
const Rec *prec2 = (const Rec *) b;
return strcmp(prec1->name, prec2->name);
}
int comp_name_desc(const void *a, const void *b)
{
const Rec *prec1 = (const Rec *) a;
const Rec *prec2 = (const Rec *) b;
return strcmp(prec2->name, prec1->name);
}
The second function should be descending order, but the result is identical: it's always in ascending order. I have checked to make sure the right function is entered at the right time. Rec is a typedef for a struct I made, which has a char * name parameter.
Also (MODIFIED to avoid overflow):
int comp_size_asc(const void *a, const void *b)
{
const Rec *prec1 = (const Rec *) a;
const Rec *prec2 = (const Rec *) b;
if (prec1->byteSize > prec2->byteSize)
return 1;
else if (prec1->byteSize < prec2->byteSize)
return -1;
else
return 0;
}
The result is completely weird, not ascending or descending (ie: 500, 515, 100, 200...). byteSize is of type off_t obtained by doing:
char *path; // Build the path
struct stat sb;
if (lstat(path, &sb) == 0) {
// Read sb.st_size
I am really not sure how to debug this. All I know, is that the appropriate compare function is entered, and that some similar compare functions used to work in the past.
Any ideas or how I can debug this would be welcome. Thank you.
EDIT:
Adding the call to qsort:
int index = 0;
Rec **array = (Rec **) malloc(sizeof(Rec *) * capacity);
// Adds element to the array...
qsort(array, index, sizeof(Rec *), comp_name_desc);
(Every time an element is added to the array, index is incremented.)
Thanks.
EDIT:
The solution was given below. Thank you!
I had to change:
const Rec *prec1 = (const Rec *) a;
to
const Rec *prec1 = *(const Rec **) a;
because of how I defined my array. Thanks!