Double pointer const-correctness warnings in C

Posted by Michael Koval on Stack Overflow See other posts from Stack Overflow or by Michael Koval
Published on 2011-02-20T06:41:51Z Indexed on 2011/02/20 7:25 UTC
Read the original article Hit count: 293

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"?

© Stack Overflow or respective owner

Related posts about c

    Related posts about const