Through trial and error I managed to get the following string comparison function to work with qsort() as I intended but I don’t really understand why the asterisk is needed in the (const char*) cast expression. Can someone please dissect and explain:-
int strCompare(const void *a, const void *b) {
return strcmp((const char*)a, (const char*)b);
}
Appendix:-
void findStrings(int * optionStats, char strings[][MAX_STRING_SIZE + 1], int numStrings)
{
qsort(strings, numStrings, 21*sizeof(char), strCompare);
}
Is there a way of eliminating the call to strcmp() through strCompare() and just using strcmp() as the parameter to qsort() instead?
You need an asterisk because you want to convert a pointer to
const voidto a pointer toconst charand an asterisk designates that they are pointer types.In fact you don’t really need conversion, since
pointer to voidtype can be implicitly converted topointer to Ttype in C language, which isn’t the case for C++.