2011-11-18 40 views
1

我在這裏新來的很抱歉沒有提供所有的信息,我應該得到我需要的幫助,但在這裏。C程序結構錯誤分配類型來自int的結構節點

struct node { 
    int data; 
    struct node* next; 
    struct node* previous; 
}; 

*currentB = MultByTen(*currentB); // this is a line in another function. 
            // currentB is a struct node with data it in. 

struct node* MultByTen(struct node* current) { 
    struct node *newNode = malloc(sizeof (struct node)); 
    newNode->data = 0; 
    newNode->next = NULL; 
    while (current->next != NULL) { 
     current = current->next; 
    } 
    newNode->previous = current; 
    current->next = newNode; 
    return current; 
} 

我收到「錯誤:分配給從類型‘詮釋’型‘結構節點’,當不兼容的類型」從代碼一行我旁邊有意見。 我返回一個結構節點*,所以我不知道爲什麼我得到這個錯誤。有任何想法嗎?

-edit: currentB是一個鏈表與數據它是

struct node* currentB = malloc(sizeof(struct node)); 

有關示例的目的,是1-> 2-> 3-> 4-> NULL和什麼。我想MultByTen只是添加一個0到列表的末尾,所以它會變成1-> 2-> 3-> 4-> 0-> NULL

+1

'currentB'的定義是什麼? MultByTen的定義是什麼? –

+0

請使用[indent style](http://en.wikipedia.org/wiki/Indent_style)代碼來提高可讀性。 – outis

回答

1

我猜currentB被聲明爲struct node *currentB?您嘗試將結構的指針分配給結構的實際實例(*currentB不是指針,而是實際實例)。在這種情況下,通話也是錯誤的。

行應該是:

currentB = MultByTen(currentB); 

注意失蹤*

我可以建議您嘗試查找關於指針的教程以及如何處理它們?