[英]Ansi C linked List what am i doing wrong?
It crys that line: 它哭了这一行:
List_Node * node = (List_Node*) malloc(sizeof(List_Node));
fails on : 失败:
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 FILE : 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 : 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;
}
The VC compilers only support the C89 standard so variables must be declared at the beginning of a scope, before any other statements. VC编译器仅支持C89标准,因此必须在作用域的开头声明任何其他语句之前声明变量。
Change List_Add()
to: 将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);
...
}
You defined list node as 您将列表节点定义为
typedef struct List_Node typedef struct List_Node
then you say struct *List_Node. 然后说struct * List_Node。
The struct is unneccessary. 该结构是不必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.