I'm not sure why qsort doesn't change my array because the elements aren't in alphabetical order. Can someone help me figure out what I'm doing wrong.
char **wordlist = malloc(sizeof(char*));
int i, numwords = 0;
wordlist[0] = strdup(words[0]);
for(i = 0; i < wcount; i++)
{
wordlist = realloc(wordlist, (numwords+1)*sizeof(char *));
wordlist[numwords] = strdup(words[i]);
numwords++;
}
printf("Added %d words to the array and they are:\n", numwords);
for(i = 0; i < numwords; i++)
{
printf("%s\n", wordlist[i]);
}
qsort(wordlist, numwords, sizeof(char *), cmpstr);
for(i = 0; i < numwords; i++)
{
printf("%s\n", wordlist[i]);
}
int cmpstr(const void* a, const void* b)
{
const char* aa = (const char*)a;
const char* bb = (const char*)b;
return strcmp(aa, bb);
}