简体   繁体   English

下面关于 C 编程中的枚举是什么意思?

[英]What does the following mean with respect to enumerations in C programming?

In a book I'm reading, enumeration constants have been introduced before arrays.在我正在阅读的一本书中,在 arrays 之前已经介绍了枚举常量。 Use of arrays has been demonstrated only through a couple examples.仅通过几个示例演示了 arrays 的使用。 Following is stated:声明如下:

enum corvid { magpie, raven, jay, corvid_num};

char const * const bird [ corvid_num ] = 
{
  [raven] = "raven",
  [magpie] = "magpie",
  [jay] = "jay",
};

for ( unsigned i = 0; i < corvid_num ; ++i)
  printf ("Corvid %u is the %s\n", i, bird[i]);

This declares a new integer type enum corvid for which we know four different values.这声明了一个新的 integer 类型枚举 corvid,我们知道它有四个不同的值。

Takeaway - Enumeration constants have either an explicit or a positional value要点 - 枚举常量具有显式值或位置值

As you might have guessed, positional values start from 0 onward, so in our example we have raven with value 0, magpie with 1, jay with 2, and corvid_num with 3. This last 3 is obviously the 3 we are interested in.正如您可能已经猜到的,位置值从 0 开始,所以在我们的示例中,我们将 raven 设置为 0,magpie 设置为 1,jay 设置为 2,corvid_num 设置为 3。最后的 3 显然是我们感兴趣的 3。

Question 1:问题一:

Does [magpie] = "magpie" imply that magpie th position refers to value "magpie" . [magpie] = "magpie"是否暗示magpie th position 指的是值"magpie"

Question 2:问题2:

According to loop, how is bird[0] equal to "raven" , since this is explicit and not a positional value.根据循环, bird[0]如何等于"raven" ,因为这是明确的而不是位置值。 Also after the first iteration is [i + 1] gonna be equal to [magpie] .同样在第一次迭代之后[i + 1]将等于[magpie] In all why is loop variable's type unsigned and not corvid or enum corvid?总而言之,为什么循环变量的类型是无符号的而不是 corvid 或 enum corvid?

I think I have misunderstood enumeration constants.我想我误解了枚举常量。

Also from,也来自,

As you might have guessed, positional values start from 0 onward, so in our example we have raven with value 0, magpie with 1, jay with 2, and corvid_num with 3. This last 3 is obviously the 3 we are interested in.正如您可能已经猜到的,位置值从 0 开始,所以在我们的示例中,我们将raven设置为 0, magpie设置为 1, jay设置为 2, corvid_num为 3。最后的 3 显然是我们感兴趣的 3。

Is author right to say that raven should have value 0 and not magpie, if this is a typo, entirety of my confusion will dispense.作者是否有权说 raven 的值应该为 0 而不是 magpie,如果这是一个错字,我的全部困惑都会消失。

In the declaration char const * const bird [ corvid_num ] the item between [ ] is the size of the array.在声明char const * const bird [ corvid_num ]中, [ ]之间的项是数组的大小。 In the initializer list, [ raven ] = " raven " , the item between [ ] is a so-called designated initializer , used to initialize a particular item in the array.在初始化器列表中, [ raven ] = " raven "[ ]之间的项是所谓的指定初始化器,用于初始化数组中的特定项。

Question 1: Does ' [ magpie ] = " magpie "' imply that 'magpie'TH position refers to value "magpie".问题1:'[magpie] = "magpie"' 是否暗示'magpie'TH position 指的是值"magpie"。

Yes.是的。

Question 2: According to loop, how is bird [0] equal to "raven", since this is explicit and not a positional value.问题 2:根据循环,bird [0] 如何等于“raven”,因为这是明确的而不是位置值。

Because enums are named constants.因为枚举被命名为常量。 If an enumeration constant ( raven etc) isn't given a number explicitly, it is given one implicitly, starting at 0. raven in your example is the second item in the enum, so it gets value 1 and whenever you type raven in the source it will be equivalent to typing 1 .如果枚举常量( raven等)没有明确给出一个数字,它会隐含地给出一个,从 0 开始。在你的例子中, raven是枚举中的第二项,所以它的值是 1, raven你在source 相当于输入1

As you might have guessed, positional values start from 0 onward, so in our example we have raven with value 0, magpie with 1, jay with 2, and corvid_num with 3. This last 3 is obviously the 3 we are interested in.正如您可能已经猜到的,位置值从 0 开始,所以在我们的示例中,我们将 raven 设置为 0,magpie 设置为 1,jay 设置为 2,corvid_num 设置为 3。最后的 3 显然是我们感兴趣的 3。

This is an error in Gustedt - Modern C .这是Gustedt - Modern C中的错误。 Errata might exist?勘误表可能存在? The output from the example code is:示例代码中的 output 是:

Corvid 0 is the magpie
Corvid 1 is the raven
Corvid 2 is the jay

Notably, if we had not used designated initializers but just done this:值得注意的是,如果我们没有使用指定的初始化器,而是这样做了:

char const * const bird [ corvid_num ] = 
{
    "raven",
    "magpie",
    "jay",
};

Then the output would instead have become那么 output 将改为

Corvid 0 is the raven
Corvid 1 is the magpie
Corvid 2 is the jay

Each enum case gets the value of the previous case plus one, unless explicitly overridden, and the first case gets the value zero.每个enum案例的值都是前一个案例的值加一,除非显式覆盖,第一个案例的值为零。 So here magpie is 0 , raven is 1 , etc. (The quoted part saying otherwise is wrong.) The final case corvid_num is the count of other cases in the array, since the + 1 of that case accounts for the zero case in the beginning.所以这里magpie0raven1 ,等等(引用的部分说否则是错误的。)最后一个 case corvid_num是数组中其他 case 的计数,因为那个 case 的+ 1占了数组中的零 case开始。

The array initialisation is using C99 designated initializers, which allows assigning values to specific indices and in any order, ie, [raven] = "raven" initialises the index raven (here 1 ).数组初始化使用 C99 指定的初始化程序,它允许以任何顺序将值分配给特定索引,即[raven] = "raven"初始化索引raven (此处为1 )。 It would be possible to just traditionally list each value in order, but doing it this way allows reordering the enum cases without having to sync the changes with the array.传统上可以按顺序列出每个值,但这样做可以重新排序枚举案例,而无需将更改与数组同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM