c - Implicit Casting to Const -
this question has answer here:
why is:
int *a = 0; int const *b = a; /* totally fine. */ int **c = 0; int const **d = c; /* generates nasty warning. */
shouldn't okay implicitly cast more const type?
is there way work around it? have following array:
int **a;
and want call following functions without ugly explicit cast:
void foo1(int const **a); void foo2(int const * const *a);
it's complicated.
the reason cannot assign char ** value const char ** pointer obscure. given const qualifier exists @ all, compiler keep promises not modify const values. that's why can assign char * const char *, not other way around: it's safe ``add'' const-ness simple pointer, dangerous take away. however, suppose performed following more complicated series of assignments:
const char c = 'x'; /* 1 */ char *p1; /* 2 */ const char **p2 = &p1; /* 3 */ *p2 = &c; /* 4 */ *p1 = 'x'; /* 5 */
assigning char ** const char ** (as in line 3, , in original question) not dangerous. sets situation in p2's promise--that ultimately-pointed-to value won't modified--cannot kept.
(c++ has more complicated rules assigning const-qualified pointers let make more kinds of assignments without incurring warnings, still protect against inadvertent attempts modify const values. c++ still not allow assigning char ** const char **, let away assigning char ** const char * const *.)
in c, if must assign or pass pointers have qualifier mismatches @ other first level of indirection, must use explicit casts (e.g. (const char **) in case), although always, need such cast may indicate deeper problem cast doesn't fix.