2014-01-27 40 views
-1

我在解析參數到函數list_append()時遇到問題。混淆我的主要問題是指針結構裏面的結構裏面的指針...試圖將結構指針作爲節點發送到函數

該函數是否要求數據類型爲「LIST」,並向它傳遞一個指針?

當我嘗試編譯此我得到以下錯誤:

請解釋一下像我5.

錯誤

In file included from main.c:3:0: 
list.h:9:7: note: expected 'LIST' but argument is of type 'struct post *' 
void list_append (LIST l, int item); 
    ^

list.h

void list_append (LIST l, int item); 

的main.c

#include <stdio.h> 

#include "list.h" 

int main() { 

static struct post { 
    char* str; 
    struct post* next; 
    int item; 
} head = { 0, NULL }; 

    struct post *p = &head; 
    struct post post; 

    list_append(p, post.item); 

} 

list.c

void list_append(struct node* n, int item) 
{ 

    /* Create new node */ 
    struct node* new_node = (struct node*) malloc (sizeof (struct node)); 
    new_node->item = item; 


    /* Find last link */ 
    while (n->next) { 
     n = n->next; 
    } 

    /* Joint the new node */ 
    new_node->next = NULL; 
    n->next = new_node; 
} 

回答

0

是。我的猜測是編譯器正在尋找數據類型LIST,正如你在struct post *中傳遞的那樣。無論如何,LIST是什麼?你有沒有在任何地方定義它?

此外,頭文件和實際函數定義中定義的函數的數據類型不匹配。

+0

是的...不,我沒有在任何地方定義'LIST',只在頭文件中。 – user3241763

+0

沿着「typedef xxxx LIST;」尋找一些東西或者可能是#define LIST xxxx併發布它,以便我們可以看到該類型已被定義爲。 – Jmc

+0

另外我沒有看到任何定義的結構節點。標題中的LIST是什麼意思? – pvkc

0

既然你不隨地定義列表,你可以改變你的函數的聲明中list.h以下幾點:

void list_append (struct node* n, int item); 

你實現一個鏈表,讓你的「名單「實際上只是指向第一個節點的指針。

否則,也許更清潔,您可以在您的頭以下:

typedef struct node * LIST; 

,改變你的函數的聲明(在.c文件)到以下幾點:

void list_append(LIST n, int item) 
+0

我現在改變了它,list.h有'typedef struct node * LIST;',list.c有'void list_append(LIST n,int item)'。我仍然得到同樣的錯誤。 – user3241763