2 ms·
You don't actually need a cast to write a qsort comparison function, because void * is implicitly converted to and from other pointer types in C. Eg: int co
by caf 8y ago
You don't actually need a cast to write a qsort comparison function, because void * is implicitly converted to and from other pointer types in C. Eg:
int compare_xyzzy(const void *a, const void *b)
{
const struct xyzzy *sa = a;
const struct xyzzy *sb = b;
return (sa->i > sb->i) - (sa->i < sb->i);
}
On the other hand, pointer casts are needed if you want to write a function like strchr() (where you can pass a const-qualified pointer, but want to return an unqualified pointer for the case where an unqualified pointer was passed).
- saagarjha 8y agoD'oh! You're right, of course.