Typedef struct vs struct? |定义差异| [英] Typedef struct vs struct? |Definition difference|

查看:120
本文介绍了Typedef struct vs struct? |定义差异|的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下块在 main() 之外,并且在每个函数(全局作用域)之前

第一段:

The following blocks are outside of main() and before every function (global scope)

1st block:

struct flight{
int number;
int capacity;
int passengers;
};

与编写}var相比,您可以创建数组,指针,变量; (仅定义此自定义数据类型的一个变量(结构 flight ))

With this you can create array,pointer,variable in contrast with writing }var; (which defines only one variable of this custom data type (struct flight))

第二个区块:

typedef struct flight{
int number;
int capacity;
int passengers;
}flight;

声明此操作将创建数据类型飞行,而不必始终编写结构飞行
我的问题是为什么typedef需要在代码块的末尾再次编写Flight?
这有点令人困惑(看起来只是该数据类型的变量)

Declaring this creates a data type flight without having to write struct flight all the time
My question is why typedef needs flight to be written a second time at the end of a block?
which is a bit confusing (it looks like only a variable of that datatype)

推荐答案

我的问题是,为什么typedef需要在代码块的末尾再次编写Flight?

My question is why typedef needs flight to be written a second time at the end of a block?

声明时:

typedef struct flight{
    int number;
    int capacity;
    int passengers;
 }flight;

您实际上声明了两件事:

you actually declare two things:

  • 一种新的结构类型struct flight
  • struct flight的类型别名flight.
  • a new structure type struct flight
  • a type alias name flight for struct flight.

为什么像任何普通声明一样,在声明的末尾出现带有typedef的类型别名的原因是因为出于历史原因,typedef与存储类说明符放在了相同的说明符类别中(例如auto).

The reason why the type alias name with typedef appears at the end of the declaration like for any ordinary declaration is because for historical reasons typedef was put in the same specifiers category as storage-class specifiers (like static or auto).

请注意,您可以声明:

typedef struct {
    int number;
    int capacity;
    int passengers;
}flight;

如果仅打算使用类型标识符flight,则不带标签名称.

without the tag name if you intend to only use the type identifier flight.

这篇关于Typedef struct vs struct? |定义差异|的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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