typedef枚举:一个成员的不同值

时间:2018-07-20 12:34:31

标签: c enums typedef

我想知道是否可以像这样向typedef枚举的同一成员分配不同的值...

typedef enum {

    START   = 0x01,
    DATA    = 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
    CRC     = 0x0A, 0x0B

}bytes_t;

...所以我以后可以像这样使用它:

bytesReceived++;                        // incremented for every received byte

switch (bytesReceived) {

    case START:
        dPkg->startByte = dpkgRxBuffer[bytesReceived];              
        break;

    case DATA:
        dPkg->data = dpkgRxBuffer[bytesReceived];
        break;

    case CRC:
        dPkg->crc = dpkgRxBuffer[bytesReceived];
        break;

}

因此,如果在循环中调用,该程序将执行DATA情况八次。这样行吗?这只是第一个主意,因此很遗憾,我无法为您提供更多背景信息。

5 个答案:

答案 0 :(得分:3)

那是不可能的。枚举是一个具有不同值的常量。
使用多种情况:

typedef enum {
    START   = 0x01,
    DATA_2  = 0x02, 
    DATA_3  = 0x03,
    DATA_4  = 0x04,
    ...
    DATA_9  = 0x09,
    CRC_1   = 0x0A, 
    CRC_2   = 0x0B
}bytes_t;

switch (variable) {
    case START:
        dPkg->startByte = dpkgRxBuffer[bytesReceived];              
        break;
    case DATA_2:
    case DATA_3:
    ...
    case DATA_9:
        dPkg->data = dpkgRxBuffer[bytesReceived];
        break;
    case CRC_1:
    case CRC_2:
        dPkg->crc = dpkgRxBuffer[bytesReceived];
        break;
}

或在范围内使用if

typedef enum {
    START   = 0x01,
    DATA_START  = 0x02, 
    DATA_STOP   = 0x09,
    CRC_1     = 0x0A, 
    CRC_2     = 0x0B
}bytes_t;

if (variable == START) {
        dPkg->startByte = dpkgRxBuffer[bytesReceived];
} else if (DATA_START <= variable && variable <= DATA_STOP) {
        dPkg->data = dpkgRxBuffer[bytesReceived];
} else if (variable == CRC_1 || variable == CRC_2) {
        dPkg->crc = dpkgRxBuffer[bytesReceived];
}

答案 1 :(得分:2)

正如@Jacajack所提到的,您不能将多个值分配给一个枚举成员变量,但是您可以通过使用切换逻辑来实现相同的目的。 byteReceived具有多个值将导致执行相同的逻辑。我希望下面的代码片段会有所帮助。

switch (bytesReceived) {

    case START:
        dPkg->startByte = dpkgRxBuffer[bytesReceived];              
        break;

    case 0x02: //List all the cases, for which same action needs to be executed.
    case 0x03:
    case 0x04:
    case 0x05:
    case 0x06:
        dPkg->data = dpkgRxBuffer[bytesReceived];
        break;

    case CRC:
        dPkg->crc = dpkgRxBuffer[bytesReceived];
        break;

}

答案 2 :(得分:0)

enum可能不是您想要的。我会这样:

#define START   0x01
#define DATA    0x02
#define CRC     0x0A
#define INVALID 0x0C

if (bytesReceived >= INVALID){
    // throw error
} else if (bytesReceived >= CRC){
    dPkg->crc = dpkgRxBuffer[bytesReceived];
} else if (bytesReceived >= DATA){
    dPkg->data = dpkgRxBuffer[bytesReceived];
} else if (bytesReceived >= START){
    dPkg->startByte = dpkgRxBuffer[bytesReceived];
} 

这样,您可以编辑范围,而不必每次都更改大量的switch语句。甚至还有更多系统的方法可以执行此操作,但是对于您所介绍的简单应用程序来说,就足够了。

答案 3 :(得分:0)

每个enum名称只能有一个值。这是您可以使用的一些替代方法。

imans77在评论中指出,您可以使用if语句代替switch

if ( bytesReceived == 0x01 ) handle_start( );
else if ( bytesReceived < 0x0a ) handle_data( );
else handle_crc( );

或者,如果您真的想保留enumswitch,可以按照此处其他人的指示进行:

enum
{
   START = 0x01,
   DATA0 = 0x02,
   DATA1 = 0x03,
   ...
   DATA7 = 0x09,
   CRC1 = 0x0a,
   CRC2 = 0x0b
} byte_t;

switch ( bytesReceived )
{
   case START: handle start( ); break;

   case DATA0:
   case DATA1:
   case DATA2:
   ...
   case DATA7:
        handle_data( );
        break;

   case CRC1:
   case CRC2:
        handle_crc( );
        break;
}

我希望这会有所帮助。

答案 4 :(得分:0)

enum中的每个名称都必须具有单个值。一个符号不能一次代表多个值。

typedef enum {
    START   = 0x01,
    DATA2   = 0x02,
    DATA3   = 0x03,
    DATA4   = 0x04,
    DATA5   = 0x05,
    DATA6   = 0x06,
    DATA7   = 0x07,
    DATA8   = 0x08,
    DATA9   = 0x09,
    CRCA    = 0x0A,
    CRCB    = 0x0B,
}bytes_t;

您可以在switch语句中执行的操作是利用案例区分法来使多个案例执行相同的操作:

switch (bytesReceived) {

    case START:
        dPkg->startByte = dpkgRxBuffer[bytesReceived];              
        break;

    case DATA2:
    case DATA3:
    case DATA4:
    case DATA5:
    case DATA6:
    case DATA7:
    case DATA8:
    case DATA9:
        dPkg->data = dpkgRxBuffer[bytesReceived];
        break;

    case CRCA:
    case CRCB:
        dPkg->crc = dpkgRxBuffer[bytesReceived];
        break;

}