int CmpFunc(const void* _a, const void* _b)
// you've got to explicitly cast to the correct type
const float* a = (const float*) _a;
const float* b = (const float*) _b;
if(*a > *b) return 1; // first item is bigger than the second one -> return 1
else if(*a == *b) return 0; // equality -> return 0
else return -1; // second item is bigger than the first one -> return -1
They use a '_' character as the first character of variable _a, I know this is a way to declare a variable. My question is: what is the advantage of this declaration? Please tell me, provide links or suggest books if any. Thanks !
So that the variables a and b can be used in the body of the function, corresponding to _a and _b, but with the desired type. There is no advantage since the author could just have easily used x and y, lhs and rhs, a_ and b_, etc.
In fact, the is a potential disadvantage: in C++, names that begin with an underscore are reserved to the implementation for use in the global namespace, and names that begin with an underscore followed by an uppercase letter (or that contain consecutive underscores) are reserved to the implementation for any use. So, _a and _b are okay here, but not okay everywhere.