2012-04-13 58 views
2

我有下面的代碼無疑需要,一些澄清在結構指針

我有一個函數如下,

void deleteNode(struct myList ** root) 
{ 
    struct myList *temp; 
    temp = *root; 
    ...//some conditions here 
    *root = *root->link; //this line gives an error 
    *root = temp->link; //this doesnt give any error 
} 

所以就是這兩條線之間的差異,對我來說看起來是一樣的.. 的錯誤是,

error #2112: Left operand of '->' has incompatible type 'struct myList * *' 

謝謝:)

+2

你讀過什麼好的C編程書?看起來你並沒有很好地理解指針的概念,在這裏解釋它會花費太多的時間和空間。先仔細閱讀一本好的C編程書。 – 2012-04-13 06:28:40

回答

7

這裏的問題在於「 - >」運算符比「*」運算符的綁定更緊密。所以,你的第一條語句:

// what you have written 
*root->link; 

正在評估到:

// what you're getting - bad 
*(root->link); 

而不是:

// what you want - good 
(*root)->link; 

由於根是一個指針的指針, - >運算符不作任何意義上,因此錯誤消息。

+0

+1,明確解釋 – 2012-04-13 06:31:24

+0

謝謝:)我馬上意識到:) – 2012-04-13 06:32:40