我无法理解这个简单代码的意义
我正在做这项作业,有一些东西(来自启动材料)我无法理解。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使
b
成为类型a
的别名,例如两个
bar
是等效的。在您的情况下,使
Course
成为类型Point[NUM_POINTS]
的别名(其中NUM_POINTS == 4
),因此是等效的。
Makes
b
an alias for typea
, e.g.both
bar
s are equivalent. In your case,Makes
Course
an alias for typePoint[NUM_POINTS]
(whereNUM_POINTS == 4
), soare equivalent.
由于
NUM_POINTS
是枚举中的最后一个条目,因此它具有最高值,并且是其他值的计数。如果 NUM_POINTS 不打算用作 Point 的实际值,那么最后一行的目的似乎是为大小等于点数量的点数组创建类型名称。 “真实”点。这里有一个很好的功能:如果您向枚举添加更多值(例如
NORTH_EAST
、SOUTH_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. IfNUM_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.) beforeNUM_POINTS
, the typedef line will automatically still be correct, because the value ofNUM_POINTS
will have grown because of the new values inserted before it.枚举从 0 开始,每个值加 1。
所以你有:
NORTH = 0
,EAST = 1
,SOUTH = 1
,WEST = 3
,NUM_POINTS = 4
NUM_POINTS
设置为枚举中的项目数。最后一行为包含 4 个元素的点数组创建了 Course 的别名。语法有点混乱,因为数组下标位于
Course
之后,而不是Point
旁边。然而,它的工作方式与例如相同:
[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 toPoint
.However it does work the same way as for example:
The
[10]
part is next to the variable name not the type.这意味着 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.