Hello, everyone. Here is the barebones of my code. I am trying to iterate the strings pointed to by argv, to parse options etc. My code never modifies the argv array, nor the strings pointed to by it, and I want the compiler to enforce that (and to provide an optimization hint to the compiler).
Code:
void fn(int argc, const char *argv[])
{
}
int main(int argc, char *argv[])
{
fn(argc, argv);
}
This compiles with gcc 5.4.0 with a warning:
Code:
test.c: In function ‘main’:
test.c:7:12: warning: passing argument 2 of ‘fn’ from incompatible pointer type [-Wincompatible-pointer-types]
fn(argc, argv);
^
test.c:1:6: note: expected ‘const char **’ but argument is of type ‘char **’
void fn(int argc, const char *argv[])
I can use (const char **) when passing argv, but that feels like a sledgehammer that can mask problems. What's the correct solution?
Richard