Reputation: 4338
I have written some piece of code which is giving me the error . The code is as follows :-
long long int compare (const void * a, const void * b)
{
return ( *(long long int*)a - *(long long int*)b );
}
long long int number;
long long int *ar =(long long int *)(malloc(sizeof(long long int)*number));
//Took the values of number and ar from and then performed the following
qsort(ar,number,sizeof(long long int),compare);
This code results into following error :-
invalid conversion from long long int (*)(const void*, const void*)' to int (*)(const void*, const void*) initializing argument 4 of void qsort(void*, size_t, size_t, int (*)(const void*, const void*))'
what am i doing wrong here ?
Upvotes: 3
Views: 1525
Reputation: 10096
long long compare (const void * a, const void * b)
{
return ( *(long long*)a - *(long long*)b );
}
In addition to not being compatible with qsort
, this type of comparison in general is only guaranteed to work with sufficiently small integers (or more generally, integers that are never too far apart). Prefer this way instead:
int compare(const void *a, const void *b)
{
long long rhs = *static_cast<const long long*>(a),
lhs = *static_cast<const long long*>(b);
return (rhs > lhs) - (lhs > rhs);
}
Upvotes: 2
Reputation: 8207
void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
Function that compares two elements. The function shall follow this prototype:
int comparator ( const void * elem1, const void * elem2 );
turn function to
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
cant I do this for long long int??
No,this function has contents same as the Standard C library header
<stdlib.h>
.Only function signature has been replaced by two declaration.(N3337/ § 25.5).
extern "C" void qsort(void* base, size_t nmemb, size_t size,
int (*compar)(const void*, const void*));
extern "C++" void qsort(void* base, size_t nmemb, size_t size,
int (*compar)(const void*, const void*));
why should not return int while taking long long int.
The very time you do something like this
a=8589934592,b=4294967296;
you will know it was better to declare a & b as int.
Upvotes: -1
Reputation: 3841
qsort expects a method with return type int, not long long.
And because straight forward casting to int might actually screw up your comparator function, you should probably do something like this (assuming you've already casted a and b to long long):
return a < b ? -1 : (a == b ? 0 : 1);
in order to conform to the requirements, i.e:
The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.
Upvotes: 5
Reputation: 1109
As others said, your function needs to return an int. The qsort function doesn't know the type of elements it sorts, hence the pointers to void in its signature. You need to return a value that is positive, when the left argument is bigger than the right one, zero when they are equal and a negative value when the left argument is smaller than the right one. You don't need to provide some exact answer.
Upvotes: 1
Reputation: 3461
Change
long long int compare (const void * a, const void * b)
to
int compare (const void * a, const void * b)
Return value of this function is always int regardless type of your array.
Upvotes: 0
Reputation: 12983
The 4th argument of void qsort(void*, size_t, size_t, int (*)(const void*, const void*))'
is a pointer to a function which signature is int (*)(const void*, const void*))
.
You are giving it a function of signature long long int (*)(const void*, const void*)
, which differs because its return type is not the right one, it should be an int
.
Upvotes: 2