views:

257

answers:

3

Hi,

as its legal to convert a pointer to non-const to a pointer to a const.. similarly,why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const) i.e,

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

Thanks.

+10  A: 

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
AProgrammer
+3  A: 

Ignoring your code and answering the principle of your question, see this entry from the comp.lang.c FAQ: Why can't I pass a char ** to a function which expects a const char **?

And as your question is tagged C++ and not C, it even explains what const qualifiers to use instead.

jamesdlin
And from the better-known C++ faq: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17
Potatoswatter
+5  A: 

Just since nobody has posted the solution, here:

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

(http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17 for why)

Potatoswatter