Domanda

    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"};

È stato utile?

Soluzione

When qsorting an array of T, your comparison function must convert its const void* pointers to const T*, because T can't be taken by value.

If words is an array of char* or char const *, you have to convert the arguments to char* const * or char const * const * respectively, it's natural when said this way.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top