-1

So I have got this piece of code:

int compare(const void *c1, const void *c2)
{
    assert(c1 && c2);
  char a = *(const char*)c1;
  char b = *(const char*)c2;
  if (a > b) return 1;
  if (a < b) return -1;
  return 0;
}

What it basically does is it gets two pointers c1 and c2 of any data type and checks who is bigger than the other. I would like this to work for any data type I recieve but it seems to make problems when I'm sending double or float.
What should I add/change?

LamaEu
  • 71
  • 6
  • 1
    `checks who is bigger than the other` that's not _exactly_ what it does. – Sourav Ghosh Apr 25 '16 at 14:40
  • 2
    Possible duplicate of [How to make generic function using void \* in c?](http://stackoverflow.com/questions/13469381/how-to-make-generic-function-using-void-in-c) - And many other requests for dirty hacks the like. – too honest for this site Apr 25 '16 at 14:43
  • @SouravGhosh: Worse: It is implementation-defined. – too honest for this site Apr 25 '16 at 14:46
  • 2
    Is this supposed to be a callback for `qsort` or some similar function? It's simply not possible to write a "generic" one; it's fundamental to the design of `qsort` that while it does not know the data type being sorted or how to order the values, you have to, meaning that you always have to write a specific comparison function. – Steve Summit Apr 25 '16 at 15:15
  • 1
    What if `c1` and `c2` point to structs? How can there possibly be a single, generic algorithm to compare them? – Steve Summit Apr 25 '16 at 15:20

1 Answers1

2

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.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • How can I use a function like that so that it would fit any type of data type I recieve? – LamaEu Apr 25 '16 at 15:34
  • @LamaEu. You cannot, because you cannot know ahead of time the data type of the sorting requirements, or even if ascending or descending. And as commented above, suppose you have a struct array and you wish to sort on the second field. Or worse, sort on the third field if the second fields are equal. – Weather Vane Apr 25 '16 at 15:41
  • Note: with cleaver use of `_Generic()` and `qsort_s()`, code can be written that handles _many_ basic types. Yet as you answer, no fully general solution. – chux - Reinstate Monica Apr 25 '16 at 16:06