1

Why is:

int *a = 0;
int const *b = a;  /* Totally fine. */

int **c = 0;
int const **d = c; /* Generates a nasty warning. */

Shouldn't it be okay to implicitly cast to a more const type?

Is there any good way to work around it? Basically I have the following array:

int **a;

And I want to call the following functions without an ugly explicit cast:

void foo1(int const **a);
void foo2(int const * const *a);
Imbue
  • 3,897
  • 6
  • 40
  • 42
  • Conceptually nothing wrong with calling `foo2`. It is just a bug in C's design that a cast is required here. – M.M Apr 10 '15 at 03:39

1 Answers1

5

It's complicated.

The reason that you cannot assign a char ** value to a const char ** pointer is somewhat obscure. Given that the const qualifier exists at all, the compiler would like to help you keep your promises not to modify const values. That's why you can assign a char * to a const char *, but not the other way around: it's clearly safe to ``add'' const-ness to a simple pointer, but it would be dangerous to take it away. However, suppose you performed the 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 a char ** to a const char ** (as in line 3, and in the original question) is not immediately dangerous. But it sets up a situation in which p2's promise--that the ultimately-pointed-to value won't be modified--cannot be kept.

(C++ has more complicated rules for assigning const-qualified pointers which let you make more kinds of assignments without incurring warnings, but still protect against inadvertent attempts to modify const values. C++ would still not allow assigning a char ** to a const char **, but it would let you get away with assigning a char ** to a const char * const *.)

In C, if you must assign or pass pointers which have qualifier mismatches at other than the first level of indirection, you must use explicit casts (e.g. (const char **) in this case), although as always, the need for such a cast may indicate a deeper problem which the cast doesn't really fix.

http://c-faq.com/ansi/constmismatch.html

maelswarm
  • 1,163
  • 4
  • 18
  • 37