Search code examples
c++pointersconstantsconst-correctness

Why isn't it legal to convert "pointer to pointer to non-const" to a "pointer to pointer to const"


It is legal to convert a pointer-to-non-const to a pointer-to-const.

Then why isn't it legal to convert a pointer to pointer to non-const to a pointer to pointer to const?

E.g., why is the following code illegal:

char *s1 = 0;
const char *s2 = s1; // OK...
char *a[MAX]; // aka char **
const char **ps = a; // error!

Solution

  • From the standard:

    const char c = 'c';
    char* pc;
    const char** pcc = &pc;   // not allowed
    *pcc = &c;
    *pc = 'C';                // would allow to modify a const object