c - ‘names’ 的存储大小未知

标签 c struct typedef forward-declaration

我在编译这个 .c 源文件时遇到这个错误

/INIT_SOURCE_BUILD/src/names_list.c:7: error: storage size of ‘names’ isn’t known

#include <stdio.h>
#include "list.h"

int main(){

    struct  List names;
    names->size = 3;

    struct ListElmt michael;
    struct ListElmt john;
    struct ListElmt adams;

    names->head = michael;

    michael->data = 12;
    michael->next = john;
    john->data = 14;
    john->next = adams;
    adams->data = 16;

    struct ListElmt pointer = List->head;
    for(int x = 0; x < 3 ; x++){
        printf("Iteration.%d data: %d", x, pointer->data);
        pointer->next = pointer->next->next;
    }
}

这是链表的头部

#ifndef LIST_H
#define LIST_H

#include <stdio.h>

/*                                      Define linked list elements*/

typedef struct _ListElmt{

void                *data;
struct _ListElmt        *next;

} ListElmt;

/*                                      Define a structure for the list*/

typedef struct _List{

int                 size;
int                 (*match)(const void *key1, const void *key2);
void                (*destroy)(void *data);

ListElmt             *head;
ListElmt             *tail;

} List;

void list_init(List *list, void (*destroy)(void *data));

void list_destroy(List *list);

int list_ins_next(List *list, ListElmt *element, const void *data);

int list_rem_next(List *list, ListElmt *element, void **data);

int list_size(const List *list);

ListElmt *list_head(const List *list);

ListElmt *list_tail(const List *list);

int list_is_head(const ListElmt *element);

int list_is_tail(const ListElmt *element);

void *list_data(const ListElmt *element);

ListElmt *list_next(const ListElmt *element);
#endif

最佳答案

当你 typedef 一个像这样的 struct 时,你不必在声明它时使用 struct :

List names;

代替

struct List names;

它也不是指针,所以 names->size 应该是 names.size

关于c - ‘names’ 的存储大小未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4045550/

相关文章:

c - C中动态数组的排序算法

c++ - 是否存在绝对需要 typedef 的情况?

c - 在函数中传递指向结构类型的指针

创建 4D 查找表

c - 在添加字符数组之前先对其进行移位

c# - 为什么我更喜欢枚举而不是具有常量值的结构

c++ - 将任何数据类型传递给 C++ 中的函数

c - 收到 "bus error 10"错误消息?

c - 使用 Pipe() 时,子进程如何向父进程返回两个值?

c++ - 这个 typedef 定义是什么意思?