简体   繁体   English

如何在C中使用枚举类型结构?

[英]How do I use enumerate type structs in C?

I am trying to set a Nokia LCD screen to turn all pixels off, all pixels on, inverse mode on and normal mode on, depending on which button the user pushes. 我试图将诺基亚LCD屏幕设置为关闭所有像素,打开所有像素,打开反向模式和打开正常模式,具体取决于用户按下的按钮。 I have all the code complete apart for setting the mode of the LCD screen. 我将所有代码分开设置了LCD屏幕的模式。 This is because they are displayed as enumerated type structs and I am not familiar with either concepts. 这是因为它们显示为枚举类型结构,并且我对这两个概念都不熟悉。 The struct is: 结构为:

typedef enum lcd_display_mode_t {
    lcd_display_all_off = 0b000,
    lcd_display_all_on  = 0b001,
    lcd_display_normal  = 0b100,
    lcd_display_inverse = 0b101,
} lcd_display_mode_t;

My best guess is that, being a enumerate type, I would simply have to type: 我最好的猜测是,作为枚举类型,我只需要键入:

if SWITCH X IS ON{
    lcd_display_mode_t = 0;
}

Which would set the display mode to lcd_display_all_off. 它将显示模式设置为lcd_display_all_off。 Is this the correct use of structs in this context? 在这种情况下,这是对结构的正确使用吗? If not, what would I type to set the display modes? 如果没有,我将键入什么设置显示模式?

An enum is not a struct. 枚举不是结构。 Using enums for storing binary data is a bad idea. 使用枚举存储二进制数据是一个坏主意。 One gets all kinds of weird side effects such as the type used being a signed int - which in turn is entirely unsuitable for the kind of hardware-related programming it will be used for. 一个人会得到各种奇怪的副作用,例如使用的是带符号的int类型-反过来,这完全不适合它将用于的与硬件相关的编程。 In addition, binary literals are not even standard C. 此外,二进制文字甚至都不是标准C。

Note that the typedef makes lcd_display_mode_t a type, not a variable. 请注意,typedef使lcd_display_mode_t成为类型,而不是变量。 Whoever wrote the code was a bit confused, it would be sufficient to just write typedef enum { ... } lcd_display_mode_t; 谁写的代码有点困惑,只写typedef enum { ... } lcd_display_mode_t;就足够了typedef enum { ... } lcd_display_mode_t; .

They intended you to use the code like this: 他们打算让您使用如下代码:

lcd_display_mode_t mode;
...
mode = lcd_display_all_off;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM