0

Trying to compare voids for a sorting algorithm. I have this so far but it kinda defeats the purpose IMO if you cast them to ints. Is there a way to compare voids? My professor ran out of time and how were stuck scowering the web. Any help is appreciated. Thanks

int fcmp(const void *one, const void *two)
{
    if (*(int*)one > *(int*)two) return 1;
    if (*(int*)one < *(int*)two) return -1;
    return 0;
}
4
  • What does "comparing voids" mean? Void is basically nothing -- and in a void *, means the absence of a specific type -- so how would C++ know what to compare?
    – cHao
    Oct 24, 2012 at 2:47
  • 1
    void is an incomplete type, so you cannot dereference a void *. Hence, there's no way to compare voids. And since this is tagged C++, shouldn't you be using std::sort. Templates allow you to avoid this kind of void * ugliness.
    – Praetorian
    Oct 24, 2012 at 2:48
  • @Praetorian: +1 for mentioning templates. I was so about to, but. :) Sounds like this is a programming exercise, though...so it's not surprising that reinventing stuff like std::sort is part of it.
    – cHao
    Oct 24, 2012 at 2:52
  • The phrase "My professor" is an indication that this is from a programming class, probably on using older technology to demonstrate a particular technique that a programmer may run into especially when maintaining older source code. It is a perfectly legitimate question. Oct 24, 2012 at 3:01

3 Answers 3

2

Assuming that the idea is to use fcmp in the context of qsort, your code is perfectly valid.

Since qsort does not care for the returned value to be 1 or -1 and would take any positive or negative number, this version is even shorter, but would work with qsort just as well:

int fcmp (const void * one, const void * two)
{
    return ( *(int*)one - *(int*)two );
}

The reason qsort uses void* is to let you use the same algorithm with different data types.

1
  • 3
    With your shortcut if one is postive, and two is negative, you could overflow, and invoke UB.
    – Dave S
    Oct 24, 2012 at 2:54
2

You are not comparing "voids" in this code. The code is casting the void pointers into int pointers, and then dereferencing the result, which means you're comparing the ints to which the pointers point to. Ideally, the function would have been written like this:

int fcmp(const int *one, const int *two)
{
    if (*one > *two) return 1;
    if (*one < *two) return -1;
    return 0;
}

But it isn't written like that since fcmp() in this case needs to have a specific signature. Otherwise it couldn't be used in a generic way. As a callback to another function, for example.

1

This looks to be a standard comparison function used in several of the standard library functions like qsort() in which you call the function with some kind of array of data items along with the comparison function that indicates whether two elements are equal to each or or not and if not what is their collating order.

So what the void pointer points to is kind of up to the programmer. That is the purpose of using the void pointers in the comparison function interface because the function, such as qsort(), which is calling the comparison function just wants to know what order two array elements are to go in. It does not know what the array elements are or how to do the comparison, it just knows the starting address of the array and the size of each element and how many elements are there.

Another function from the standard library is the bsearch() function.

So to use this you might have code like the following: see qsort() man page.

typedef struct {
   int iValue;
   char  sName[10];
} DataValue;

// compare two elements of the array and indicate which one is higher
// or lower in collating sequence or if they are equal.
int dataComp (void *one, void *two)
{
    return ((DataValue *)one)->iValue - ((DataValue *)two)->iValue;
}

int main (int argc, char *argv[])
{
    DataValue myData[25];
   //.. put some data stuff in the array.
   // call qsort with the array.  specify number of elements and size of each one
   qsort (myData, sizeof(myData)/sizeof(myData[0]), sizeof(myData[0]), dataComp);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.