So it seems like it means a pointer to a constant pointer to char. That is it points to a char * const, so far so good.
What gets me confused is where and how I saw it used. I was looking at the man page for qsort and the example does the following to convert the pointers to elements of a char ** (an array of strings), (pointers to elements seen as const void *) to normal char pointers feedable to strcmp:
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);
}
My question is, why is there a cast to char * const *? Why isn’t it just a const char ** (because eventually we want to send a const char * to strcmp)?
char * const *indeed means a pointer to a constant pointer to chars. The reason this cast is performed in the code in the question is the following:p1andp2are (non-const) pointers to a constant location. Let’s assume the type of this location isconst T.Now we want to cast
p1andp2to their real types. We know that each element of the array is achar *, thereforeT = char *. That isconst Tis a constant pointer to char, which is written aschar * const.Since
p1andp2are pointers to the elements of the array, they are of typeconst T *, which ischar * const *.Since the function merely calls
strcmp, in truth it wouldn’t have made any difference if the parameters were cast tochar **orconst char **orconst char * const *or whatever.