Hi all,
i was reading in a book how to use the function qsort. the function needs a pointer to a compare function comp.
sth like this was done :
Code:
char *data[MAX]; // memory was allocated and so on
qsort(data, MAX, sizeof(data[0]), comp); // the call of qsort
.
.
.
int comp(const void *s1, const void *s2)
{
return (strcmp(*(char **)s1, *(char **)s2)); // what i don't understand
}
why in our exaplme we were using so many "*"
i understand that (char **) is a typecasting, and the other one for dereferencing the pointer...
so far when i wanted to compare two strings pointed to by ptr1 and ptr2 i used
strcmp(ptr1, ptr2).
i mean the comp function will be given pointers to strings, why not only :
Code:
return (strcmp((char *)s1, (char *)s2)); // typecasting from void* to char*