I am having a problem getting qsort to work for an array of strings (char * to be exact). I create a compare function that I thought should work for the qsort requirement, but it does not seem to work at all. I also need it to work with whitespace characters and blank strings (ex. ""). Any direction or note on what I am doing wrong would be greatly appreciated.
My relevant source code:
int compareAlphabetically(const void * a,const void * b);
void sortStringArray(char * * arrString, int len){
int size = sizeof(arrString) / sizeof(char *);
if(*arrString != NULL && len > 1)
qsort(arrString, size, sizeof(char *), compareAlphabetically);
}
int compareAlphabetically(const void * a, const void * b)
{
const char *a_p = *(const char **) a;
const char *b_p = *(const char **) b;
return strcmp(a_p,b_p);
}
The function definition is (and it should remain unchanged):
/**
* Takes an array of C-strings, and sorts them alphabetically, ascending.
*
* arrString: an array of strings
* len: length of the array 'arrString'
*
* For example,
* int len;
* char * * strArr = explode("lady beatle brew", " ", &len);
* sortStringArray(strArr, len);
* char * str = implode(strArr, len, " ");
* printf("%s\n", str); // beatle brew lady
*
* Hint: use the <stdlib.h> function "qsort"
* Hint: you must _clearly_ understand the typecasts.
*/
void sortStringArray(char * * arrString, int len);