Possible Duplicate:
why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const)
Why do I get a warning (gcc 42.2) with the following call of foo?
void foo(const char **str)
{
(*str)++;
}
(...)
char **str;
foo(str);
(...)
I understand why we cannot call a function that excepting a char ** with a const char **, but the opposite seems ok to me, so why the following warning?
warning: passing argument 1 of 'foo' from incompatible pointer type
It is wrong. There is no real room for arguing with the compiler here, since it’s supported by the spec. Here’s an example which explains exactly why it is wrong:
If the compiler didn’t spit out an error, your program would probably crash because you’d be overwriting a string literal (which is often marked read-only in memory).
So you cannot implicitly cast
char **toconst char **because it would allow you to remove theconstqualifier from any value — basically, it would allow you to ignoreconstat will without an explicit cast.The main reason the compiler gives a warning instead of an error is because lots of old code does not use the
constqualifier where it would if the code were written today.