繁体   English   中英

Ansi C链表我在做什么错?

[英]Ansi C linked List what am i doing wrong?

它哭了这一行:
List_Node * node = (List_Node*) malloc(sizeof(List_Node));

失败:

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);

    ...
}

您将列表节点定义为

typedef struct List_Node

然后说struct * List_Node。

该结构是不必要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM