This can't be done in C. C has no run-time type information (RTTI).
What you're asking is to compare some number of bytes pointed to by c1
to some number of bytes pointed to by c2
. We don't even know how many bytes there are, let alone what type they're supposed to represent. So there's no way to write a single, "generic" function which can magically decide which of a potentially infinite number of comparison algorithms to apply.
Addendum: If it were possible to do this, qsort
would have been designed with a function like this built in, and you wouldn't have to write your own comparison function. The fact that the designers of qsort
punted on this responsibility, and kicked the problem of writing a comparison function back to you, the user, pretty much proves that it can't be done.
It's an unfortunate fact of life that whenever you call qsort
, you have to write your own custom comparison function, pretty much every time; you can't write one general-purpose one, once, and be done with it.