[英]Define an array with enum variable as array size
What does it mean to define an array with enum variable as array size? 将枚举变量定义为数组大小的数组意味着什么?
For example I have the following code: 例如,我有以下代码:
typedef enum
{
D_ROM_RDE_GROUP_0 = 0x0,
D_ROM_RDE_GROUP_1,
D_ROM_RDE_MAX_GROUPS
}E_ROM_RDE_GROUPS;
U_08 pPlaneCopy[D_ROM_RDE_MAX_GROUPS];
I don't understand it... 我不明白......
Thanks for the help. 谢谢您的帮助。
The first thing to remember is that enumeration values are compile-time constant. 首先要记住的是枚举值是编译时常量。 The other thing is that enumeration values (unless initialized to a specific value) increases. 另一件事是枚举值(除非初始化为特定值)增加。 So in your case D_ROM_RDE_GROUP_0
is equal to 0
, D_ROM_RDE_GROUP_1
is equal to 1
and D_ROM_RDE_MAX_GROUPS
is equal to 2
. 因此,在您的情况下, D_ROM_RDE_GROUP_0
等于0
, D_ROM_RDE_GROUP_1
等于1
, D_ROM_RDE_MAX_GROUPS
等于2
。
This means that when you declare the array, it's basically the same thing as 这意味着当你声明数组时,它基本上是相同的
U_08 pPlaneCopy[2];
The enum value D_ROM_RDE_MAX_GROUPS is actually an integer value, in your case: 2. So you are defining an array with two elements. 在您的情况下,枚举值D_ROM_RDE_MAX_GROUPS实际上是一个整数值:2。因此,您定义了一个包含两个元素的数组。 Probably the two elements will be accessed by the indices : D_ROM_RDE_GROUP_0 and D_ROM_RDE_GROUP_1 索引可能会访问这两个元素:D_ROM_RDE_GROUP_0和D_ROM_RDE_GROUP_1
This is a techique often used to define an array that is in some way related to the values in an enum. 这是一种技术,通常用于定义一个与枚举中的值相关的数组。 For a simple example: 举个简单的例子:
enum {
ZERO,
ONE,
TWO,
THREE,
MAX_VALUE //has value of 4
};
because the natural value of an enumerated element starts at zero, and increments by one for each member, the final value will always be useful to initialize an array, of any type ( strings
, ints
, floats
, etc) for a number of elements equal to that final value. 因为枚举元素的自然值从零开始,并且对每个成员递增1,所以最终值对于初始化数组总是很有用,对于许多元素来说,它们的任何类型( strings
, ints
, floats
等)都是相等的达到最终价值。 eg. 例如。
int numbers[MAX_VALUE];//an array with 4 elements
int i;
You can then use MAX_VALUE to guarantee treating the array in a loop without going beyond the bounds of the array, eg. 然后,您可以使用MAX_VALUE来保证在循环中处理数组,而不会超出数组的范围,例如。 something like: 就像是:
for(i=0;i<MAX_VALUE;i++)//will loop 4 times
{
numbers[i]=i;//assigned a value equal to its namesake, for i < MAX_VALUE
}
Given this explanation, the enum in your original post is simply being initialized with a value of 2: 鉴于此解释,原始帖子中的枚举只是初始化为值2:
U_08 pPlaneCopy[D_ROM_RDE_MAX_GROUPS];
is the same as 是相同的
U_08 pPlaneCopy[2];
An unsigned integer is automatically assigned to each enum elements if not explicitly specified otherwise. 如果没有明确指定,则自动为每个枚举元素分配无符号整数。 Thus, with your enum declaration, D_ROM_RDE_GROUP_0
equals to 0
, D_ROM_RDE_GROUP_1
equals to 1
, and D_ROM_RDE_MAX_GROUPS
equals to 2. 因此,使用枚举声明, D_ROM_RDE_GROUP_0
等于0
, D_ROM_RDE_GROUP_1
等于1
, D_ROM_RDE_MAX_GROUPS
等于2。
Thus, the declaration 因此,宣言
U_08 pPlaneCopy[D_ROM_RDE_MAX_GROUPS];
is equivalent to 相当于
U_08 pPlaneCopy[2];
Which means, you declared an array with one U_08
element corresponding to each possible value in E_ROM_RDE_GROUPS
assuming D_ROM_RDE_MAX_GROUPS
is not a value used otherwise. 这意味着,您声明了一个数组,其中一个U_08
元素对应于E_ROM_RDE_GROUPS
每个可能值,假设D_ROM_RDE_MAX_GROUPS
不是另外使用的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.