[英]Enum type Warning in C
I am writing a code on lpc1788 ARM Cortex M3. 我在lpc1788 ARM Cortex M3上编写代码。 I came across a strange warning when I tried to configure the ports as GPIO.
当我尝试将端口配置为GPIO时,我遇到了一个奇怪的警告。 Despite of the warning, the code works absolutely fine, but to learn why this warning come, I am putting forward this post here.
尽管有警告,代码工作得非常好,但要了解为什么会出现这个警告,我在这里提出这篇文章。 Following is the code that I have written.
以下是我写的代码。
static uint32_t * PIN_GetPointer(uint8_t portnum, uint8_t pinnum)
{
uint32_t *pPIN = NULL;
pPIN = (uint32_t *)(LPC_IOCON_BASE + ((portnum * 32 + pinnum)*sizeof(uint32_t)));
return pPIN;
}
void PINSEL_SetPinMode ( uint8_t portnum, uint8_t pinnum, PinSel_BasicMode modenum)
{
uint32_t *pPIN = NULL;
pPIN = PIN_GetPointer(portnum, pinnum);
*(uint32_t *)pPIN &= ~(3<<3); //Clear function bits
*(uint32_t *)pPIN |= (uint32_t)(modenum<<3);
}
int main(void)
{
PINSEL_SetPinMode(1,15,0); //this gave a warning: enumerated type mixed with another type
PINSEL_SetPinMode(1,18,PINSEL_BASICMODE_NPLU_NPDN); //this doesnt give any warning
/* Following is the enum present in a GPIO related header file, putting it here in comments so that
those who are going through this post, can see the enum
typedef enum
{
PINSEL_BASICMODE_NPLU_NPDN = 0, // Neither Pull up nor pull down
PINSEL_BASICMODE_PULLDOWN, // Pull-down enabled
PINSEL_BASICMODE_PULLUP, // Pull-up enabled (default)
PINSEL_BASICMODE_REPEATER // Repeater mode
}PinSel_BasicMode;
*/
return 0;
}
You are using int
type where enum PinSel_BasicMode
type is required. 您正在使用
int
类型,其中需要enum PinSel_BasicMode
类型。 While enums and ints are usually interchangeable, they are different types. 虽然枚举和整数通常是可以互换的,但它们是不同的类型。
Value 0
is not an enum value. 值
0
不是枚举值。 PINSEL_BASICMODE_NPLU_NPDN
is. PINSEL_BASICMODE_NPLU_NPDN
是。 It is only 0
through definition. 通过定义只有
0
。
Should the enum declaration change and PINSEL_BASICMODE_NPLU_NPDN
was equal to 1, your code would be invalid. 如果枚举声明更改且
PINSEL_BASICMODE_NPLU_NPDN
等于1,则您的代码将无效。
1. You're passing an int
where a enum
value is expected. 1.您正在传递一个
int
,其中包含enum
值。 So either, cast it to the correct enum, or better: use the correct enum
value directly: 所以要么将其转换为正确的枚举,要么更好:直接使用正确的
enum
值:
PINSEL_SetPinMode(1, 15, (PinSel_BasicMode)0);
PINSEL_SetPinMode(1, 15, PINSEL_BASICMODE_NPLU_NPDN);
2. You're using the bit shift operator on a enum
value. 2.您使用上的位移位运算符
enum
值。 I think you need to cast before and after the bit shift to make the compiler happy: 我认为你需要在位移之前和之后进行转换以使编译器满意:
void PINSEL_SetPinMode ( uint8_t portnum, uint8_t pinnum, PinSel_BasicMode modenum)
{
uint32_t *pPIN = NULL;
pPIN = PIN_GetPointer(portnum, pinnum);
*pPIN &= ~(3<<3); //Clear function bits
*pPIN |= (uint32_t)((uint32_t)modenum << 3);
}
Before: because you want to shift an integer value instead of the enum
value. 之前:因为您想要移动整数值而不是
enum
值。
After: because the output type of a shift operation is not necessarily the same as the input type. 之后:因为移位操作的输出类型不一定与输入类型相同。
Enums are defined type and hence here warning is of incomatible type with int, so you can typecast to avoid warning. 枚举是定义类型,因此这里的警告是int的不可用类型,因此您可以进行类型转换以避免警告。
But as here 0 is defined for your enum so it doesnot cause your code to give wrong result. 但是因为这里为你的枚举定义了0,所以它不会导致你的代码给出错误的结果。
Hope it helps..... 希望能帮助到你.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.