S
Spoon
Hello,
I've come across the following comparison function used as the 4th
parameter in qsort()
int cmp(const void *px, const void *py)
{
const double *x = px, *y = py;
return (*x > *y) - (*x < *y);
}
It came "as-is" i.e. no comments.
Is it good style to write comparison functions that way?
Should the return values be made more explicit? Or would it be enough to
add comments explaining why the function (usually) works?
I've come across the following comparison function used as the 4th
parameter in qsort()
int cmp(const void *px, const void *py)
{
const double *x = px, *y = py;
return (*x > *y) - (*x < *y);
}
It came "as-is" i.e. no comments.
Is it good style to write comparison functions that way?
Should the return values be made more explicit? Or would it be enough to
add comments explaining why the function (usually) works?