用C语言枚举 [英] Enumeration in C language

查看:99
本文介绍了用C语言枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事低级C函数,因此我发现很多代码很复杂。有人可以解释我这段代码吗?



 if(req.pib_attr == ZB_PHY_PIB_CURRENT_CHANNEL)





 zb_mlme_get_request_t req; 





  typedef   struct   zb_mlme_get_request_s  
{
zb_mac_pib_attr_t pib_attr;
zb_uint8_t pib_index;
} ZB_PACKED_STRUCT
zb_mlme_get_request_t;







 typedef  enum  
{
/ * PHY PIB * /
ZB_PHY_PIB_CURRENT_CHANNEL = 0x00,
ZB_PHY_PIB_CURRENT_PAGE = 0x04,
/ * MAC PIB * /
ZB_PIB_ATTRIBUTE_ACK_WAIT_DURATION = 0x40,
ZB_PIB_ATTRIBUTE_ASSOCIATION_PERMIT = 0x41,
ZB_PIB_ATTRIBUTE_AUTO_REQUEST = 0x42,
ZB_PIB_ATTRIBUTE_BATT_LIFE_EXT = 0x43,
ZB_PIB_ATTRIBUTE_BATT_LIFE_EXT_PERIODS = 0x44,
ZB_PIB_ATTRIBUTE_BEACON_PAYLOAD = 0x45,
ZB_PIB_ATTRIBUTE_BEACON_PAYLOAD_LENGTH = 0x46,
ZB_PIB_ATTRIBUTE_BEACON_ORDER = 0x47,
ZB _PIB_ATTRIBUTE_BEACON_TX_TIME = 0x48,
ZB_PIB_ATTRIBUTE_BSN =×49,
ZB_PIB_ATTRIBUTE_COORD_EXTEND_ADDRESS = 0x4a,
ZB_PIB_ATTRIBUTE_COORD_SHORT_ADDRESS = 0x4b,
ZB_PIB_ATTRIBUTE_DSN = 0x4c,
ZB_PIB_ATTRIBUTE_TIMESTAMP_SUPPORTED = 0x5c,
ZB_PIB_ATTRIBUTE_SECURITY_ENABLED = 0x5d
} zb_mac_pib_attr_t;







这里的枚举嵌套在一个结构中。



如果在此处检查条件,则针对枚举成员检查总枚举变量req.pibattr。我已检查值未分配给

 req.pib_attr 

..



如何执行条件。

解决方案

比较会将变量 req.pib_attr 中的值与 ZB_PHY_PIB_CURRENT_CHANNEL 这是0 ...



与变量与常数比较没什么不同。



  int  a =  5  ; 
if (a == 7 ){...}


您好,



我认为此链接可以帮助您。



枚举 [ ^ ]


您好



如果您想了解如何在c#中使用enum,您可以查看示例

I working on low level C functions so i find a lot of code complex . Can someone explain me this piece of code?

if (req.pib_attr == ZB_PHY_PIB_CURRENT_CHANNEL)



zb_mlme_get_request_t req;



typedef struct zb_mlme_get_request_s
{
  zb_mac_pib_attr_t  pib_attr;
  zb_uint8_t          pib_index;
} ZB_PACKED_STRUCT
zb_mlme_get_request_t;




typedef enum
{
  /* PHY PIB */
  ZB_PHY_PIB_CURRENT_CHANNEL                    = 0x00,
  ZB_PHY_PIB_CURRENT_PAGE                       = 0x04,
  /* MAC PIB */
  ZB_PIB_ATTRIBUTE_ACK_WAIT_DURATION            = 0x40,
  ZB_PIB_ATTRIBUTE_ASSOCIATION_PERMIT           = 0x41,
  ZB_PIB_ATTRIBUTE_AUTO_REQUEST                 = 0x42,
  ZB_PIB_ATTRIBUTE_BATT_LIFE_EXT                = 0x43,
  ZB_PIB_ATTRIBUTE_BATT_LIFE_EXT_PERIODS        = 0x44,
  ZB_PIB_ATTRIBUTE_BEACON_PAYLOAD               = 0x45,
  ZB_PIB_ATTRIBUTE_BEACON_PAYLOAD_LENGTH        = 0x46,
  ZB_PIB_ATTRIBUTE_BEACON_ORDER                 = 0x47,
  ZB_PIB_ATTRIBUTE_BEACON_TX_TIME               = 0x48,
  ZB_PIB_ATTRIBUTE_BSN                          = 0x49,
  ZB_PIB_ATTRIBUTE_COORD_EXTEND_ADDRESS         = 0x4a,
  ZB_PIB_ATTRIBUTE_COORD_SHORT_ADDRESS          = 0x4b,
  ZB_PIB_ATTRIBUTE_DSN                          = 0x4c,
  ZB_PIB_ATTRIBUTE_TIMESTAMP_SUPPORTED          = 0x5c,
  ZB_PIB_ATTRIBUTE_SECURITY_ENABLED             = 0x5d
} zb_mac_pib_attr_t;




Here enum is nested inside a structure .

If condition is checked here total enum variable req.pibattr is checked against a member of enum. I have checked values are not assigned to

req.pib_attr

..

How the condition will be executed .

解决方案

The comparison will compare the value in the variable req.pib_attr to the value of ZB_PHY_PIB_CURRENT_CHANNEL which is 0...

It is not much different than comparing a variable with a constant.

int a = 5;
if (a == 7) { ... }


Hi,

I think this link will help you.

Enum[^]


Hi

If you want to learn how enum work in c#, you can check this Example


这篇关于用C语言枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆