【发布时间】:2015-01-26 01:29:42
【问题描述】:
list.h
#ifndef LIST_H
#define LIST_H
/* Function prototypes */
struct nodeStruct* List_createNode(int item);
#endif
list.c
#include <stdio.h>
#include <stdlib.h>
struct nodeStruct {
int item;
struct nodeStruct *next;
};
struct nodeStruct* List_createNode(int item) {
struct nodeStruct *node = malloc(sizeof(struct nodeStruct));
if (node == NULL) {return NULL;}
node->item = item;
node->next = NULL;
return node;
}
Main.c:
#include "list.h"
#include <assert.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct nodeStruct *one = List_createNode(1);
while(one != NULL) {
printf("%d", one->item); //error
one= one->next; //error
}
错误:error: dereferencing pointer to incomplete type printf("%d", one->item);
错误在one->item,我尝试了几种组合来取消引用,但似乎不起作用。什么是正确的方法?
更新:
list.h
#ifndef LIST_H
#define LIST_H
struct nodeStruct {
int item;
struct nodeStruct *next;
};
/* Function prototypes */
struct nodeStruct* List_createNode(int item);
#endif
现在的错误是,invalid application of ‘sizeof’ to incomplete type ‘struct nodeStruct’
struct nodeStruct *node = malloc(sizeof(struct nodeStruct));
来自我的 list.c 文件。
【问题讨论】:
-
您是否在主 .c 文件中包含了
struct nodeStruct的定义?此外,您可能希望删除one = *one->next;中的 *。哦,修改one可能会泄漏内存。 -
@EOF 这不是问题,因为其他方法也有效。
-
所以
struct nodeStruct的定义实际上main() 不可用。将struct nodeStruct的定义放入list.h。提示:目前在list.c。 -
...现在你还需要
#include "list.h"inlist.c。 -
实际上,您的
int main()函数在哪里?它不在您发布的main.c中
标签: c pointers gcc linked-list