15

Possible Duplicate:
Why can’t I convert ‘char**’ to a ‘const char* const*’ in C?

I am curious, why can't I pass a char ** to const char ** function? Where as it is OK to pass char * to a const char * function it seems not to be OK to do it with double pointers. I thought it was always ok to add constness (but not ok to drop constness) but now it seems I have been wrong.

Gcc compiler is giving me the errror:

note: expected ‘const char **’ but argument is of type ‘char **’

Here is the code snippet:

int f(const char **a) { }

int main() {
    char *a;
    f(&a);
}

Any ideas?

Community
  • 1
  • 1
bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • This appears to be similar to http://stackoverflow.com/questions/78125/why-cant-i-convert-char-to-a-const-char-const-in-c – Eric Finn Aug 16 '10 at 18:24
  • 2
    BTW, to people voting to close: this isn't an exact duplicate of http://stackoverflow.com/questions/78125/why-cant-i-convert-char-to-a-const-char-const-in-c since that one is specifically about `const char* const*`. (Some of the answers there are relevant, however.) – jamesdlin Aug 17 '10 at 03:23

1 Answers1

19

Because the compiler can't guarantee safety.

See Q11.10 from the comp.lang.c FAQ: Why can't I pass a char ** to a function which expects a const char **?

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 */

In line 3, we assign a char ** to a const char **. (The compiler should complain.) In line 4, we assign a const char * to a const char *; this is clearly legal. In line 5, we modify what a char * points to--this is supposed to be legal. However, p1 ends up pointing to c, which is const. This came about in line 4, because *p2 was really p1. This was set up in line 3, which is an assignment of a form that is disallowed, and this is exactly why line 3 is disallowed.

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.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • This is wrong: "In line 4, we assign a const char * to a const char *; this is clearly legal." `*p2` is a `char*` and `&c` is a `const char*`. In the statement `*p2 = &c` there is an assignment of a `const char*` to `char*` this is not legal without a constant cast. – Jonathan Mee Jul 16 '18 at 17:31
  • @JonathanMee No. `p2` is of type `const char**`; it is a pointer to a pointer to a `char` constant. `*p2` is of type `const char*`. – jamesdlin Jul 16 '18 at 20:28
  • Ugh, you're right. It's line 3 that 's the problem. It seems like preventing that would really solve the problem... – Jonathan Mee Jul 17 '18 at 12:35
  • @JonathanMee Yes, and that is why line 3 is disallowed. =) – jamesdlin Jul 17 '18 at 21:44