2012-02-18 66 views
0

它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; 
} 
+1

在頭文件,則跳過用於'List_Node'結構的'typedef'。另外,當列表爲空時添加節點時,不要設置This-> last。 – 2012-02-18 12:36:40

回答

1

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

0

您定義的列表節點

typedef結構List_Node

那你說結構* List_Node。

該結構是不必要的。