[英]Why am I not getting an error (or at least warning) when I pass a number to a function that is expecting an enum?
I'm using gcc for an ARM Cortex M4 and doing the following: 我将gcc用于ARM Cortex M4并执行以下操作:
uint32_t SetGyroInterruptBits(enum Gyr_Int_Set_Bits BitMap)
{
// Code Here
return RelevantValue;
}
I created the enums as follows: 我创建了如下枚举:
enum Gyr_Int_Set_Bits
{
HR_FILT = 0x80,
AM_FILT = 0x40,
HR_Z_AXIS = 0x20,
HR_Y_AXIS = 0x10,
HR_X_AXIS = 0x08,
AM_Z_AXIS = 0x04,
AM_Y_AXIS = 0x02,
AM_X_AXIS = 0x01,
};
When I call this function as follows: 当我如下调用此函数时:
SetGyroInterruptBits(34);
I expected to get at least a warning telling me I should be passing an enum. 我希望至少得到一个警告,告诉我我应该通过一个枚举。 However, it compiles completely happily.
但是,它可以完全愉快地编译。 I thought this was an advantage of enums over just using a #define.
我认为这是枚举相对于仅使用#define的优势。 Is it a warning you have to specifically ask the compiler to check for or do I just misunderstand?
是您必须专门要求编译器进行检查还是只是误解我的警告? Is using a #define just as good in this instance?
在这种情况下使用#define是否一样好?
In C an enumeration is just a symbolic compile-time constant of type int
. 在C中,枚举只是
int
类型的符号编译时常量。 You can assign any int
value to an enumeration, even if it's not a valid value for the enumeration. 您可以将任何
int
值分配给枚举,即使它不是该枚举的有效值。
Passing the integer value 34
is the same as passing HR_Z_AXIS | AM_Y_AXIS
传递整数值
34
与传递HR_Z_AXIS | AM_Y_AXIS
HR_Z_AXIS | AM_Y_AXIS
. HR_Z_AXIS | AM_Y_AXIS
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.