声明一系列结构 [英] Declare an array of structures

查看:145
本文介绍了声明一系列结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个结构数组,但似乎出现此错误:

I am trying to create an array of structures but it appears this bug:

错误,数组类型为不完整的元素类型

"Error, array type as incomplete element type"

typedef struct {
char data[MAX_WORD_SIZE];     
int total_count;       
Occurrence_t reference[MAX_FILES];    
int nfiles;         
} Word_t;

struct Word_t array[a];


推荐答案

TL; DR

更改结构定义

struct Word_t {
char data[MAX_WORD_SIZE];     
int total_count;       
Occurrence_t reference[MAX_FILES];    
int nfiles;         
};

或者(也不能同时使用)数组声明:

Or (and not both), the array declaration:

Word_t array[a];






您所做的是定义一个未命名的结构,为您提供了一个带有typedef的备用名称。没有结构Word_t ,仅定义了 Word_t 类型。

标记名称空间(在 struct / union / 之后使用的名称枚举驻留)与全局命名空间(文件范围 typedef 驻留的名称所在)是分开的。

The tag namespace (where the names that you use after struct/union/enum reside) is separate from the global namespace (where file scope typedefed names reside).

许多程序员认为,使用 struct标记类型名称很麻烦,您应该始终对结构名称进行typedef。 其他认为这是对typedef-ing的滥用,并且该关键字传达了很多意义对于他们来说,typedef是语法糖,没有任何真正的抽象。

Many programmers feel that lugging around a struct tag type name is cumbersome, and you should always do a typedef for the struct name. Others feel this is an abuse of typedef-ing, and that the keyword conveys a lot of meaning; to them the typedef is syntactic sugar without any real abstraction.

您选择以哪种方式编写代码,坚持使用它,并且不要混淆两个名称空间。

Whichever way you choose to write you code, stick to it, and don't confuse the two namespaces.

这篇关于声明一系列结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆