为什么在传递参数时使用const会给我一个警告?

为什么这段代码会给我一个警告:从不兼容的指针类型传递“test”的参数1? 我知道这是关于char之前的const,但为什么呢?

void test(const int ** a) { } int main() { int a=0; int *b=&a; int **c=&b; test(c); return 0; } 

你不能将int **分配给const int ** ,因为如果你这样做,后一个指针将允许你给一个int *变量一个const int对象的地址:

 const int myconst = 10; int *intptr; const int **x = &intptr; /* This is the implicit conversion that isn't allowed */ *x = &myconst; /* Allowed because both *x and &myconst are const int * ... */ /* ... but now intptr points at myconst, and you could try to modify myconst through it */ 
 const int ** 

是指向const int的指针,但是您传递指向int的指针

我想你可能想用int ** const声明测试,它表示指针是const而不是值。

注意:我认为这应该放在关于C中指针的每个问题中:cdecl.org是一种非常好的方式来提供一个人类可读的表达式

这个问题的第二个答案可能会有所帮助:

为什么我不能在C中将’char **’转换为’const char * const *’?

不幸的是,接受的答案并不是很好,并没有解释原因。