2011-09-08 66 views
2

爲什麼這段代碼給我一個警告說:從不兼容的指針類型中傳遞「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://stackoverflow.com/questions/78125/why-cant-i-convert-char-to-a-const-char-cons t-in-c) –

回答

5

不能分配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 **」
0

第二這個問題的答案可能會有所幫助:

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

不幸的是,公認的答案不是很好,所以沒有任何解釋的原因。

+0

答案顯示順序現在是隨機的,並且所有者可以更改接受哪個答案,因此您可能需要說明您引用了哪些答案。我也會說這個問題不是特別相關:這個問題是關於將'int **'轉換爲'const int **'的情況(注意缺少第二個'const') –

2
const int ** 

是一個指針,指向一個const int的,但你傳遞一個指針的指針爲int

我想你可能要與int ** const申報測試,它說,指針是常量而不是值。

注:我覺得這應該放在關於C指針的每一個問題:cdecl.org是給人一種人類可讀表達一個很不錯的辦法

相關問題