I am trying to pass a parameter to a function, and indicate that the parameter should be considered const
by the receiving function.
It was my understanding that the following code example shows the only way to ensure that the test
function can be called with the argv
variable, which is not declared as const.
void test(const char * const *arr);
int main(int argc, char *argv[], char *env[])
{
test(argv);
}
void test(const char * const *arr)
{
}
However, gcc gives me a warning such as the following:
E:\Projects\test>gcc -o test test.c
test.c: In function 'main':
test.c:5:2: warning: passing argument 1 of 'test' from incompatible pointer type
[enabled by default]
test.c:1:6: note: expected 'const char * const*' but argument is of type 'char **'
This leads me to believe that what I am doing here is somehow wrong.
Is there a better way to pass a parameter to a function, and indicate that it should be considered const
by the receiving function?
The C FAQ explains why in C there is a safe guard when converting a pointers to non-const
to const
. In your case of a doubly const
qualified target pointer the problem that is described there wouldn't occur, I think. So C++ has a relaxed rule that would allow the implicit conversion that you are asking for. Unfortunately for C the standards committee found the rule from C++ too complicated, so you'd have to cast your pointer to the expected type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With