PengHQ said:
int compare(const void *x,const void *y)
{
strcmp(*(char **)x,*(char **)y);
Note the dereference here.
}
why not code like these:
int compare(const void *x,const void *y)
{
strcmp(x,y);
Note the absence here.
Think about it this way: bsearch (for example) might be used
to search an array of pointers to char, and most of the time
you will search based on what the pointers are pointing to
(strings for example). This is all well and good, but sometimes
you might want to search based on the actual pointer (or ints
maybe or whatever is in the array). To allow for both
possibilites bsearch passes in pointers to the elements
actually in the array (which would be the pointers to char).
This means that what is passed to the compare function is a
pointer to pointer to char converted to a pointer to void.