This code snippet is mainly from man page of qsort.
int cmp(const void *p1, const void *p2)
{
char s1 = *(*(char * const *)p1);
char s2 = *(*(char * const *)p2);
return s1 - s2;
}
int main(int argc, char *argv[])
{
int j;
printf("Before Qsort: \n");
for(j = 1; j <argc ; j++)
printf("%s ", argv[j]);
printf("\n");
qsort(&argv[1], argc - 1, sizeof(char *), cmp);
printf("After Qsort: \n");
for(j = 1; j <argc ; j++)
printf("%s ", argv[j]);
printf("\n");
}
I can understand I pass the address of pointer to char array which is &argv[1] in the qsort However, I am confused why I have to do the following cast to get the value?
*(*(char * const *)p1)
I also checked other questions in stackoverflow, it is easy when we pass 1-D array. So, I can use
type var = *(const type *)p1 to get the value
Looking into this example, the type cast looks so weird to understand.
Would you please help me to understand the meaning of every piece cast to reach the final value?
Thanks.
*(type *)p1in it, wheretypeisconst char*. - n. 1.8e9-where's-my-share m.