I need to use the std::qsort() function to sort variables in a vector of a data member of a class. So this is supposed to exercise the use of extern "C" to pass the function pointer into std::qsort().
Honestly, I'm at a complete loss, sltsort() works fine, but I'm not sure what to do to get sqsort() to work. Here is what I have:
I don't have time to properly review your code, but the compareDescending() function takes void pointers, but the thing you are sorting is not supposed to be addresses but ints.
@chenqi07 - I wont pretend to know enough about coding to give a good explanation. But I think it's because std::qsort() is a C function and it needs the function it points to to be external?
@Duoas - Thanks! This might just do the trick, I was hitting that wall of exhaustion. I'll give it a shot and let you all know what happens.
@Duoas - I'm in your debt, this worked beautifully. However, if you have a moment, could you give a brief explanation as to what is going on here? I'm assuming you are doing some sort of dereferencing? I want to make sure I understand in case I have to use something like this again.
What is happening is casting the 'void pointer' to a 'const int pointer' and then dereferencing it.
Void pointer can point to anything. So your parameter can be any type of pointer. However in your case since you are explicitly casting it to an int* you need to make sure you pass it a valid datatype.
The qsort() function(s) don't care what your data type is. But your comparison functions do... however, C has no generics outside of requiring your comparision function to take void pointers as arguments. But your comparison function is specific to type anyway, so inside the function you can just cast the arguments to the correct type.