Using qsort in C we pass in a comparison function e.g.
int cmp(const void*, const void*);
the protoype of qsort expects a int (* )(const void* , const void*)
so we call:
qsort(..., cmp);
but it is equally valid to call:
qsort(..., &cmp);
and this is what we would have to do if we passed in a static member-function in C++. Kernighan & Ritchie (2nd Edition, 5.11 "Pointers To Functions" p119) states that "since [cmp] is known to be a function, the & operator is not necessary, in the same way that it is not needed before an array name."
Does anyone else feel slightly uncomfortable with this (esp. regarding type-safety)?
Whether you feel uncomfortable or not doesn't change the fact that C is not considered a type-safe language. Case in point:
int main()
{
int integer = 0xFFFFFF;
void (*functionPointer)() = (void(*)())integer;
functionPointer();
return 0;
}
This is completely valid at compile time, but it is obviously not safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With