From the manpage for qsort
:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
The
qsort()
function sorts an array withnmemb
elements of sizesize
. Thebase
argument points to the start of the array.The contents of the array are sorted in ascending order according to a comparison function pointed to by
compar
, which is called with two arguments that point to the objects being compared.The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined.
So, what qsort()
will do is call the comparison function you specify, each time pointing to two 'somethings', that something being held in an array. It will then swap the 'somethings' around if it wants.
There are two obvious ways to make this work.
The first and perhaps most obvious (but least useful) way)is that you could have a two dimensional array of chars, and the qsort could swap one line of chars with another (in which case each of the arrays of chars would need to be of fixed length size
, including the terminating zero, and all the strings would need to abut eachother exactly, so that it can index the start of the n
th string at offset n * size
.
The second, more subtle, but more useful way is for your array to be a list of pointers to the strings you want to sort. Then qsort
just swaps around the pointers.
Your main problem here is that you are doing neither. In lines like:
qsort(t,100,sizeof(char),compare1);
you are saying the size of each entry is sizeof(char)
, i.e. one character long. So qsort
will think you are sorting an array of only 100 characters. That isn't going to work.
As this is your homework, I think I should leave you to it from here.