The below program sorts all the suffices of a string using qsort() library function.
int sacomp(const void *a, const void *b)
{
return strcmp(*(const char**)a, *(const char**)b); <------------
}
void sort(string s)
{
int size = s.size();
char const *data = s.c_str();
char const **sa = new char const *[size+1];
for(int i = 0; i < size; i++)
sa[i] = data+i;
qsort(sa, size, sizeof(sa[0]), sacomp); // O(n * Lon n)
}
int main()
{
string s("ABCCCDEFABBABBA");
sort(s);
return 0;
}
I am not able to understand the casting done in the sacomp() method.
strcmp(*(const char**)a, *(const char**)b);
Why a is casted to const char** and then being de-referenced?
Your element is
char *, therefore you should change void onchar *and get pointer tochar *, which ischar **. Meanwhile strcmp needschar *.