I have the following variable.
char **arr
Then I want to perform some modification on array which means it can't be declared as a constant.
Now I have a function that accepts the argument of type const char ** arr
. But I have no control over the signature of this function.
Now when I cast arr
to const char ** arr
g++ generates a warning which is [-Werror=cast-qual]
.
For more clarification consider following MCVE:
#include<cstdio>
void print(const char** x){
printf("%s", x[0]);
}
int main(int argc, char **argv){
if(argc>1){
print((const char **)argv);
}
return 0;
}
//Then compile it as follow:
$ g++ -Wcast-qual test.cpp
//gives the following output:
MCVE.cpp: In function ‘int main(int, char**)’:
MCVE.cpp:5:36: warning: cast from type ‘char**’ to type ‘const char**’ casts away qualifiers [-Wcast-qual]
const char ** q = (const char**) argv;
So my question is why this generates a warning? Is there any risk in doing this?
And how to achieve a behavior I want to achieve?