I am trying to use a C program (via dynamic libraries) with Python and the ctypes module. Several constants defined in a header file will be important for me, but I am unsure of how enum
is being used to set their values.
我正在尝试使用Python和ctypes模块的C程序(通过动态库)。头文件中定义的几个常量对我来说很重要,但我不确定如何使用枚举来设置它们的值。
The obvious ones, I think I understand like: enum{THING1, THING2, THING3};
显而易见的,我想我明白了:enum {THING1,THING2,THING3};
THING1=0, THING2=1, THING3=3
THING1 = 0,THING2 = 1,THING3 = 3
But, what about this? enum{THING1=-1, THING2, THING3};
但是,这个怎么样?枚举{THING1 = -1,THING2,THING3};
Is the result: THING1=-1, THING2=1, THING3=2
?
结果是:THING1 = -1,THING2 = 1,THING3 = 2?
What about this one? enum{THING1=1, THING2, THING3, THING4=-1}
?
这个如何?枚举{THING1 = 1,THING2,THING3,THING4 = -1}?
I don't have an easy way to test this, so hoping someone can explain the way enum
works in this context. C books I looked in seemed to cover either the first case or the case where each value is explicitly defined, but not this mixed case.
我没有一种简单的方法来测试它,所以希望有人可以解释enum在这种情况下的工作方式。我查看的C书似乎涵盖了第一种情况或明确定义每个值的情况,但不是这种混合情况。
Many thanks in advance!
提前谢谢了!
2 个解决方案
#1
5
The value of the first enum
constant is 0, unless specified otherwise.
除非另有说明,否则第一个枚举常量的值为0。
The value of any other enum
constant is one more than the value of the previous, unless it is explicitly specified.
除非明确指定,否则任何其他枚举常量的值都比前一个值大1。
So
所以
enum{THING1=-1, THING2, THING3};
sets THING2 = 0, THING3 = 1
, and
设置THING2 = 0,THING3 = 1,和
enum{THING1=1, THING2, THING3, THING4=-1}
sets THING2 = 2, THING3 = 3
(and THING4 = -1
is explicitly given).
设置THING2 = 2,THING3 = 3(明确给出THING4 = -1)。
#2
3
They just get incremented from the previous value. In your example,
它们只是从前一个值增加。在你的例子中,
enum{THING1=-1, THING2, THING3};
is equivalent to
相当于
enum{THING1=-1, THING2=0, THING3=1};
And this one
和这个
enum{THING1=1, THING2, THING3, THING4=-1}?
is equivalent to
相当于
enum{THING1=1, THING2=2, THING3=3, THING4=-1}
#1
5
The value of the first enum
constant is 0, unless specified otherwise.
除非另有说明,否则第一个枚举常量的值为0。
The value of any other enum
constant is one more than the value of the previous, unless it is explicitly specified.
除非明确指定,否则任何其他枚举常量的值都比前一个值大1。
So
所以
enum{THING1=-1, THING2, THING3};
sets THING2 = 0, THING3 = 1
, and
设置THING2 = 0,THING3 = 1,和
enum{THING1=1, THING2, THING3, THING4=-1}
sets THING2 = 2, THING3 = 3
(and THING4 = -1
is explicitly given).
设置THING2 = 2,THING3 = 3(明确给出THING4 = -1)。
#2
3
They just get incremented from the previous value. In your example,
它们只是从前一个值增加。在你的例子中,
enum{THING1=-1, THING2, THING3};
is equivalent to
相当于
enum{THING1=-1, THING2=0, THING3=1};
And this one
和这个
enum{THING1=1, THING2, THING3, THING4=-1}?
is equivalent to
相当于
enum{THING1=1, THING2=2, THING3=3, THING4=-1}