C语言 “名称”的存储大小未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4045550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:50:20  来源:igfitidea点击:

storage size of ‘names’ isn’t known

cstructtypedefforward-declaration

提问by Wazery

I get this error while compiling this .c source file

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

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

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

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

回答by nmichaels

When you typedefa structlike that, you don't have to use structwhen declaring it:

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

List names;

instead of

代替

struct List names;

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

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

回答by Oliver Charlesworth

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

struct List names;不声明指针,但您试图取消引用它(使用->)。使用names.size来代替。

回答by The Archetypal Paul

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

该结构称为_List。typedef 是列表。所以你要

 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

其他答案关于 . vs -> 问题