Ansi C链表我做错了什么?
问题描述:
它CRYS该行:List_Node * node = (List_Node*) malloc(sizeof(List_Node));
Ansi C链表我做错了什么?
失败上:
1>list.c(31): error C2275: 'List_Node' : illegal use of this type as an expression
1>list.c(8) : see declaration of 'List_Node'
h文件:
#ifndef _LIST_H
#define _LIST_H
typedef struct List_Node;
typedef struct List_Struct
{
unsigned int count;
struct List_Node * root;
struct List_Node * last;
int SizeOfData;
}List_Struct;
#endif
C_FILE:
typedef struct List_Node
{
void * data;
struct List_Node * next;
}List_Node;
Status List__Add (List_Struct * This,void * const item)
{
Assert(This)
Assert(item)
struct List_Node * node = (List_Node*) malloc(sizeof(List_Node));
IsAllocated(node);
node->data = malloc(This->SizeOfData);
IsAllocated(node->data);
memcpy(node->data,item,This->SizeOfData);
node->next = NULL;
if(NULL == This->root) /*if first item to be added*/
{
This->root= node;
This->last =This->root;
}
else
{
This->last->next = node;
}
return STATUS_OK;
}
答
VC编译器只支持C89标准,所以必须在任何其他语句之前在范围的开始声明变量。
更改List_Add()
到:
Status List__Add (List_Struct * This,void * const item)
{
List_Node* node;
Assert(This)
Assert(item)
/* Don't cast return type of malloc(): #include <stdlib.h> */
node = malloc(sizeof(List_Node));
IsAllocated(node);
...
}
+0
傻我。 4年的C#程序员让你忘记这些东西。 – Nahum 2012-02-18 12:39:43
答
您定义的列表节点
typedef结构List_Node
那你说结构* List_Node。
该结构是不必要的。
在头文件,则跳过用于'List_Node'结构的'typedef'。另外,当列表为空时添加节点时,不要设置This-> last。 – 2012-02-18 12:36:40