2014-01-27 35 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