简体   繁体   English

将数字传递给期望枚举的函数时,为什么没有得到错误(或至少警告)?

[英]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.

相关问题 为什么我在尝试将 'char (*c)[6]' 传递给需要 'const char (*c)[6]' 的函数时收到警告? - Why do I get a warning trying to pass a 'char (*c)[6]' to a function expecting a 'const char (*c)[6]'? 为什么在函数中使用结构和枚举时会出错? - Why am I getting error when using Structs and Enums in Function? 为什么我在 C 的枚举中收到“错误:错误指令”? - Why am I getting “Error: bad instruction” in an enum in C? 为什么在文件范围内初始化枚举类型变量时出现错误? - Why am I getting error when enum type variable is initialized in file scope? 为什么会收到“无效声明”警告? - Why am I getting a “statement with no effect” warning? 我正在尝试使用双指针将字符串传递给 function 但出现错误 - I am trying to pass a string into a function with a double pointer but getting an error 当我尝试在Linux而不是Mac上进行编译时,为什么会出现“错误:函数'fileno'的隐式声明” - Why am I getting “error: implicit declaration of function ‘fileno’” when I try to compile on Linux but not on a Mac 当我尝试使用 push 和 pop function 时,为什么会出现错误? - Why am I getting an error when I try to use push and pop function? 错误:没有以前的功能原型。 为什么我收到此错误? - Error: No previous prototype for function. Why am I getting this error? 为什么我收到函数参数的未声明错误? (C) - Why am I getting an undeclared error for a function argument? (C)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM