Susan94 Susan94 - 11 months ago 50
C++ Question

qsort does not make any changes to the integer array

I'm using qsort to sort an array of integers. But qsort is not making any changes in the array. My compare function is as follows:

int mycomp(const void *a, const void *b){
int *a1 = (int*)a;
int *b1 = (int*)b;

if ( a1 < b1 )
return -1;
else if ( a1 == b1 )
return 0;
else
return 1;
}


The array contains:

pos[M] = {40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 5, 10, 15, 20, 15, 30, 35};


And I called qsort as:

qsort(pos, M, sizeof(int), mycomp);


The output is the exact same array. Can anyone please help me?

Answer Source

You compare pointer not values.

fix:

int a1 = *(int*)a;
int b1 = *(int*)b;