我无法理解这个简单代码的意义

发布于 2024-09-02 23:12:27 字数 211 浏览 5 评论 0原文

我正在做这项作业,有一些东西(来自启动材料)我无法理解。

typedef enum
{
    NORTH,
    EAST,
    SOUTH,
    WEST,
    NUM_POINTS
} Point;

typedef Point Course[NUM_POINTS] ;

我不明白最后一行背后的想法,如何在代码中使用它?

I am doing this assignment, and there are some stuff (from start-up materials) that I cannot comprehend.

typedef enum
{
    NORTH,
    EAST,
    SOUTH,
    WEST,
    NUM_POINTS
} Point;

typedef Point Course[NUM_POINTS] ;

I don't get the idea behind the last line , and how can I use it in the code?

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

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

发布评论

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

评论(4

寒冷纷飞旳雪 2024-09-09 23:12:27
typedef a b;

使b 成为类型a 的别名,例如

typedef int foo;

int bar;
foo bar;

两个bar 是等效的。在您的情况下,

typedef Point Course[NUM_POINTS] ;

使 Course 成为类型 Point[NUM_POINTS] 的别名(其中 NUM_POINTS == 4),因此

Course baz;
Point baz[NUM_POINTS];

是等效的。

typedef a b;

Makes b an alias for type a, e.g.

typedef int foo;

int bar;
foo bar;

both bars are equivalent. In your case,

typedef Point Course[NUM_POINTS] ;

Makes Course an alias for type Point[NUM_POINTS] (where NUM_POINTS == 4), so

Course baz;
Point baz[NUM_POINTS];

are equivalent.

情定在深秋 2024-09-09 23:12:27

由于 NUM_POINTS 是枚举中的最后一个条目,因此它具有最高值,并且是其他值的计数。如果 NUM_POINTS 不打算用作 Point 的实际值,那么最后一行的目的似乎是为大小等于点数量的点数组创建类型名称。 “真实”点。

这里有一个很好的功能:如果您向枚举添加更多值(例如 NORTH_EASTSOUTH_WEST 等)之前 NUM_POINTS 时,typedef 行将自动保持正确,因为 NUM_POINTS 的值会由于在其之前插入的新值而增大。

Since NUM_POINTS is the last entry in the enum, it has the highest value, and is the count of the other values. If NUM_POINTS is not meant to be used as an actual value for a Point, it looks like the purpose of the last line is to create a type name for an array of points of size equal to the number of "real" points.

Here's one nice feature: if you add more values to the enum (like NORTH_EAST, SOUTH_WEST, etc.) before NUM_POINTS, the typedef line will automatically still be correct, because the value of NUM_POINTS will have grown because of the new values inserted before it.

空城仅有旧梦在 2024-09-09 23:12:27

枚举从 0 开始,每个值加 1。

所以你有:NORTH = 0EAST = 1SOUTH = 1WEST = 3NUM_POINTS = 4

NUM_POINTS 设置为枚举中的项目数。

最后一行为包含 4 个元素的点数组创建了 Course 的别名。语法有点混乱,因为数组下标位于 Course 之后,而不是 Point 旁边。

typedef Point Course[NUM_POINTS] ;

然而,它的工作方式与例如相同:

int x[10];  

[10] 部分位于变量名称而不是类型旁边。

an enum starts at 0 and increases by 1 for each value.

So you have: NORTH = 0, EAST = 1, SOUTH = 1, WEST = 3, NUM_POINTS = 4

NUM_POINTS is set to the number of items in the enum.

The last line creates an alias of Course for a point array with 4 elements in it. The syntax is a little confusing because the array subscript is after Course and not next to Point.

typedef Point Course[NUM_POINTS] ;

However it does work the same way as for example:

int x[10];  

The [10] part is next to the variable name not the type.

家住魔仙堡 2024-09-09 23:12:27

这意味着 Course 可用于表示 Points 数组,其中 NUM_POINTS 是数组中的项目数。

It means that Course can be used to represent an array of Points, with NUM_POINTS being the number of items in the array.

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