Double pointer const-correctness warnings in C
- by Michael Koval
You can obviously cast a pointer to non-const data to a a pointer of the same type to const data:
int *x = NULL;
int const *y = x;
Adding additional const qualifiers to match the additional indirection should logically work the same way:
int * *x = NULL;
int *const *y = x; /* okay */
int const *const *z = y; /* warning */
Compiling this with GCC or Clang with the -Wall flag, however, results in the following warning:
test.c:4:23: warning: initializing 'int const *const *' with an expression of type
'int *const *' discards qualifiers in nested pointer types
int const *const *z = y; /* warning */
^ ~
Why does adding an additional const qualifier "discard qualifiers in nested pointer types"?