qsort(words, size1, size2, compareWords);
inside compare words:
int compareWords(const void *ac, const void *bc)
this works:
char const *a = *(const char **)ac;
these don't (a
gets some garbage values):
char const *a = ac;
char const *a = (const char *) ac;
what is the rationale?
Also, in some examples I see size2
to be sizeof(char *)
. Shouldn't this be sizeof(*words)
?
words is declared as:
char *words[] = {"abc", "pqr", "abcd", "pqsl"};
When
qsort
ing an array of T, your comparison function must convert itsconst void*
pointers toconst T*
, because T can't be taken by value.If
words
is an array ofchar*
orchar const *
, you have to convert the arguments tochar* const *
orchar const * const *
respectively, it's natural when said this way.