There's a well-known warning in gcc
warning: passing argument ... from incompatible pointer type [-Wincompatible-pointer-types]
and most often it occurs when trying to do something with parameter argv
of function main
.
I've read this answer: https://stackoverflow.com/a/28702525/447503 . Ok, the C standard says so and the following code will compile with a warning:
int main(int argc, char *argv[]) {
const char * const * b = argv;
return b == 0;
}
Not sure about C++, because g++ compiles same code without warnings.
Is there any reason behind this part of the standard? Can I do something bad after the cast, for example make argv
point to non-writable memory?
This may be a duplicate of Why can't I convert 'char**' to a 'const char* const*' in C? , but Aaron's anwer is incorrect: read the comments.
Is Kevin's answer incorrect?
I'm thinking to add a convenience inline function like: const char * const *foo(char * const *a)
. Unlike C-style casts it would be type safe.