I wrote this function It should sort the strings in alphabetical order like father aunt >> aunt father. It should use only pointers.
void sort(char **arrPP[]){
int m=0;
for ( char ***p = arrPP; *p != NULL; ++p) {
int z = 0;
while (*(*p + z) != NULL) {
m++;
z++;
}
}
for(int i=0;i<m;i++) {
for (char ***p = arrPP; *p != NULL; ++p) {
int u=0;
while (*(*p + u) != NULL) {
char **x = *p + u;
int z = 1;
for (char ***c = arrPP; *c != NULL; ++c) {
while (*(*c + z) != NULL) {
if (strcmp(*x, *(*c + z)) > 0) {
char *o = *x;
*x = *(*c + z);
*(*c + z) = o;
}
z++;
}
}
u++;
}
}
}
}
It should sort arrPP.
int main ()
{
char * arrP1 [ ] = { "father", "mother", NULL};
char * arrP2 [ ] = { "sister", "brother", "grandfather", NULL };
char * arrP3 [ ] = { "grandmother", NULL};
char *arrP4 [ ] = { "uncle", "aunt", NULL };
char ** arrPP [ ] = { arrP1, arrP2, arrP3, arrP4 , NULL};
printAllStrings(arrPP );// printALL Strings prints all strings and it works .
sort(arrPP);
printAllStrings(arrPP );
return 0;
}
the output should be:
father mother
sister brother grandfather
grandmother
uncle aunt
aunt brother
father grandfather grandmother
mother
sister
uncle
But the output is
father mother
sister brother grandfather
grandmother
uncle aunt
//after sorting
brother uncle
mother sister grandmother
father
grandfather aunt
.What is wrong with the sort function ?How can I fix it or write something else than can help with my problem?
strcmp((void*)x, (void*)c)hide errors:cis achar ***,xis achar **, butstrcmptakes arguments of type (const)char *. That can't be right.zmust be set to zero for each new sublistc. (It should probably be defined locally in the loop overc.) What you want to achieve is difficult. I suggest that you create an auxiliary linearized array of strings["father", "mother", "sisret", ...], sort that and then re-assign the items of the sorted linearized list back to the original nested array.