avoliva
avoliva

Reputation: 3366

Comparing voids in c++

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;
}

Upvotes: 0

Views: 403

Answers (3)

Richard Chambers
Richard Chambers

Reputation: 17593

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);
}

Upvotes: 1

Nikos C.
Nikos C.

Reputation: 51850

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.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

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.

Upvotes: 2

Related Questions