use qsort with comparator function
Domanda
i know qsort from c library and i have implemented it with chars and integers(convert from const void * form to relevant form) but now i am implementing code which gives me longest duplicated in given sentences here is simple compare function
int pstrcmp(char **p,char **q){
return strcmp(*p,*q);
}
and i want to use it in qsort like this
qsort(a,n,sizeof(char *),pstrcmp);(a is array of strings)
when i write directly,it writes it is incompatible and son on,shows me error,please help me to correct it
error is this
2 IntelliSense: argument of type "int (*)(char **p, char **q)" is incompatible with parameter of type "int (__cdecl *)(const void *, const void *)" c:\users\datuashvili\documents\visual studio 2010\projects\duplicate_strings\duplicate_strings\duplicates_strings.cpp 32 27 duplicate_strings
Soluzione
You need to give your comparator function the correct function signature and then cast internally, e.g.
int pstrcmp(const void * p, const void * q)
{
const char **ps = (const char **)p;
const char **qs = (const char **)q;
return strcmp(*ps, *qs);
}