如何在.C文件中声明结构?

问题描述 投票:1回答:2

我必须在C中实现链接列表,并且根据项目规范,必须在我的头文件中创建以下结构:

typedef struct node {
char *string;
struct node* next;  
} 


typedef struct {
node *head;  /* could have been struct node* as well */
node *tail;
} list;

现在我如何在我的.C文件中提供这些?我已经#included Header文件,但是当我尝试调用时,例如myList.head,我不断收到错误声明我正在尝试对不是结构或联合的东西执行操作,那么我该怎么做解决这个问题?

c struct header structure declare
2个回答
3
投票

在你的第一个struct之后你需要一个分号。要么摆脱你的typedef,或给它一个名字。


3
投票

你的typedef是错的。语法是:

typedef [some_type_definition] [type_name];

类型定义是这样的:

struct node {
    char *string;
    struct node* next;
};

所以你需要在它前面添加typedef,并在node后面加上它(在分号前面)。这将允许您使用nodestruct node引用结构。

对于你的列表,你没有命名结构,但你做了typedef它。这意味着你不能把它称为struct list - 你必须只使用list。您可以根据需要命名结构。

© www.soinside.com 2019 - 2024. All rights reserved.