问题描述
我正在尝试将一个参数传递给一个函数,并且指出参数应该被接收函数视为 const 。
I am trying to pass a parameter to a function, and indicate that the parameter should be considered const by the receiving function.
我的理解是,下面的代码示例显示了确保 test 函数可以用 argv调用的唯一方法变量,它没有声明为const。
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) { }
然而,gcc给了我一个警告,如下所示:
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 **'
这让我相信我在这里所做的是某种错误。
是否有更好的方法将参数传递给一个函数,并指出它应该被接收函数视为 const ?
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?
推荐答案
这篇关于将非const参数传递给需要const参数的函数时出现警告。有没有更好的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!