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

问题描述:

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

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

可能重复的“常量char * const *'in C?](http://*.com/questions/78125/why-cant-i-convert-char-to-a-const-char-cons t-in-c) –

不能分配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 */ 
的[我为什么不能转换“的char **”

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

Why can't I convert 'char**' to a 'const char* const*' in C?

不幸的是,公认的答案不是很好,所以没有任何解释的原因。

+0

答案显示顺序现在是随机的,并且所有者可以更改接受哪个答案,因此您可能需要说明您引用了哪些答案。我也会说这个问题不是特别相关:这个问题是关于将'int **'转换为'const int **'的情况(注意缺少第二个'const') –

const int ** 

是一个指针,指向一个const int的,但你传递一个指针的指针为int

我想你可能要与int ** const申报测试,它说,指针是常量而不是值。

注:我觉得这应该放在关于C指针的每一个问题:cdecl.org是给人一种人类可读表达一个很不错的办法