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

ashishsony picture ashishsony · Feb 8, 2010 · Viewed 7k times · Source

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!

Answer

AProgrammer picture AProgrammer · Feb 8, 2010

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