You have defined a cmp function, and on the qsort line, you have named the compare function.
But you have not put in a cmp function!
I'm giving you a bit of a rough handling because I object to people taking code from here, there, wherever, on the internet, and not having done the studying for it.
As soon as I get my jaw to quit grinding my teeth, I'll post up something helpful.
Code:
/* qsort help */
Sorts using the quicksort algorithm.
Syntax:
void qsort(void *base, size_t nelem, size_t width, int(*fcmp)
(const void *, const void *));
Prototype in:
stdlib.h
Remarks:
qsort is an implementation of the "median of three" variant of the quicksort
algorithm.
qsort sorts the entries in a table by repeatedly calling the user-defined
comparison function pointed to by fcmp.
þ base points to the base (0th element) of
the table to be sorted.
þ nelem is the number of entries in the
table.
þ width is the size of each entry in the
table, in bytes.
*fcmp, the comparison function, accepts two arguments, elem1 and elem2, each
a pointer to an entry in the table.
The comparison function compares each of the pointed-to items (*elem1 and
*elem2), and returns an integer based on the result of the comparison.
*elem1 < *elem2 fcmp returns an integer < 0
*elem1 == *elem2 fcmp returns 0
*elem1 > *elem2 fcmp returns an integer > 0
In the comparison, the less-than symbol (<) means the left element should
appear before the right element in the final, sorted sequence.
Similarly, the greater-than symbol (>) means the left element should appear
after the right element in the final, sorted sequence.
Return Value: None.
Portability:
qsort is available on UNIX systems and is defined in ANSI C.
See Also:
bsearch lsearch
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_function( const void *a, const void *b);
char list[5][4] = { "cat", "car", "cab", "cap", "can" };
int main(void)
{
int x;
qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x < 5; x++)
printf("%s\n", list[x]);
return 0;
}
int sort_function( const void *a, const void *b) //this is the actual compare function!
{
return( strcmp(a,b) );
}