“名称”的存储大小;未知

发布于 2024-09-30 08:01:26 字数 1798 浏览 5 评论 0原文

编译此 .c 源文件时出现此错误

/INIT_SOURCE_BUILD/src/names_list.c:7: 错误:“名称”的存储大小不是 已知

#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

I get this error while compiling this .c source file

/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;
    }
}

and here is header of this linked list

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯℡ 2024-10-07 08:01:26

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

List names;

而不是

struct List names;

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

When you typedef a struct like that, you don't have to use struct when declaring it:

List names;

instead of

struct List names;

It's also not a pointer, so names->size should be names.size.

喵星人汪星人 2024-10-07 08:01:26

struct List name; 没有声明指针,但您试图取消引用它(使用 ->)。请改用names.size

struct List names; doesn't declare a pointer, but you're trying to dereference it (using ->). Use names.size instead.

哆啦不做梦 2024-10-07 08:01:26

该结构称为_List。 typedef 是 List。所以你想要

 List names;

或者

struct _List names; /* probably not, the _ is convention for internal names */

你的行正在声明一个尚未定义的“结构列表”。

关于 的其他答案非常正确。对比->问题

The struct is called _List. The typedef is List. So you want

 List names;

or

struct _List names; /* probably not, the _ is convention for internal names */

Your line is declaring a "struct List" which isn't defined yet.

The other answers are quite correct about the . vs -> issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文