我知道,从隐式转换char **到const char **无法做到的,为什么,而且转化为char *const *作品.请参阅底部以获取有关该说明的链接.
除了一件特别的事情外,这一切都是有道理的 所以我有以下代码:
#include <stdio.h>
void
print(const char *const*param)
{
printf("%s\n", param[0]);
}
int
main(int argc, char **argv)
{
print(argv);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我将其编译为C++代码,它编译得非常好.但是,如果相同的代码仅编译为C代码,我会收到错误(好吧,警告,但我们假设-Werror,即将警告视为错误).
GCC:
test.c: In function ‘main’:
test.c:12:11: warning: passing argument 1 of ‘print’ from incompatible pointer type [-Wincompatible-pointer-types]
print(argv);
^
test.c:4:1: note: expected ‘const char * const*’ but argument is of type ‘char **’
print(const char *const*param)
^
Run Code Online (Sandbox Code Playgroud)
铛:
test.c:12:11: warning: passing 'char **' …Run Code Online (Sandbox Code Playgroud)