Re:A bug story: data alignment in C on x86 (2016)
by caf on Tuesday September 04, 2018 @10:02PM
(#17914547)
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).
Re:A bug story: data alignment in C on x86 (2016)
by saagarjha on Wednesday September 05, 2018 @02:25AM
(#17915485)
D'oh! You're right, of course.
Re:A bug story: data alignment in C on x86 (2016)
by v_lisivka on Wednesday September 05, 2018 @06:25PM
(#17922398)
If you want to write "generic" function in C, use #define to generate static function for your specific type. But it much much better to use C++ or Rust instead.
Example: https://svnweb.freebsd.org/base/head/sys/sys/tree.h?revision...
Nope. There's a reason that void * exists. How would you write a "generic" function like qsort without a pointer cast?