Y
Yoshi
Hello,
I encountered following code when I was reading man page of qsort.
In the function compstringp, it casts p1 and p2 to ( char * const * ).
I re-read the C-Faq and still don't understand what it means.
Does ( char * const * ) means, cast to "pointer to const pointer to
char"? Does it make sense to cast this way ?
static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int
main(int argc, char *argv[])
{
int j;
assert(argc > 1);
qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
Thank you
I encountered following code when I was reading man page of qsort.
In the function compstringp, it casts p1 and p2 to ( char * const * ).
I re-read the C-Faq and still don't understand what it means.
Does ( char * const * ) means, cast to "pointer to const pointer to
char"? Does it make sense to cast this way ?
static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int
main(int argc, char *argv[])
{
int j;
assert(argc > 1);
qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
Thank you