2

I have defined the following enum:

typedef enum {
    F105 = 0x00,
    F164 = 0x10,
    F193 = 0x20,
    F226 = 0x30,
    F227 = 0x40
}BOARD_TYPE;

To make the code readable, I would like to use the enum name when using one of its members. Like this:

void do_work(uint8_t board_type) {
    if (board_type == BOARD_TYPE.F164) {
        // Do stuff...
    }
}

Now, this doesn't compile. I get an error message "Expected expression before 'BOARD_TYPE'".

So what would be the proper way of using a enum member while also referring to the enum name to increase code readability?

Oystein
  • 1,232
  • 11
  • 26

4 Answers4

7

enum is a list of values, an "enumeration". It is not a struct/container class with members.

Now what you should do for clarity is to only compare enumeration constants of a given type with variables of the same type. Not against uint8_t as in your example.

This is pretty much self-documenting code:

void do_work (BOARD_TYPE board_type) {
    if (board_type == F164) {
        // Do stuff...
    }
}

Good compilers can be configured to give warnings when comparing enums against wrong types. Otherwise you can also create type safe enums with some tricks.

You can also prefix all enum constants to indicate what type they belong to - this is common practice:

typedef enum {
    BOARD_F105 = 0x00,
    BOARD_F164 = 0x10,
    BOARD_F193 = 0x20,
    BOARD_F226 = 0x30,
    BOARD_F227 = 0x40
}BOARD_TYPE;
Lundin
  • 195,001
  • 40
  • 254
  • 396
2

enum is not a structure and the member names are just names of the corresponding constants so you cant access enums elements via .

Change

BOARD_TYPE.F164

to

F164

enum constants are of type int so board_type will expand to int.


For better readability

typedef enum {
    BOARD_F105 = 0x00,
    BOARD_F164 = 0x10,
    BOARD_F193 = 0x20,
    BOARD_F226 = 0x30,
    BOARD_F227 = 0x40
}BOARD_TYPE;

Its always better to pass an enum type like

// Function definition
void do_work(BOARD_TYPE board_type) {
    if (board_type == BOARD_F164) {
        // Do stuff...
    }
}

// Calling
do_work(BOARD_F164);
kocica
  • 6,412
  • 2
  • 14
  • 35
  • Yes, obviously. But then when reading the code later I lose the knowledge that F164 is a member of the enum "BOARD_TYPE". – Oystein Aug 25 '17 at 08:00
  • So use it in name of constans ? `BOARD_F164` – kocica Aug 25 '17 at 08:01
  • Yes, that's possible. But I would still like to indentify in the code which enum the value is defined in. @Lundin had a good approach on this. But thank you for your input. – Oystein Aug 25 '17 at 08:04
  • @Oystein Isnt that the case with other types also? How do you remember what is type of your variables while reading code? Also F164 is "not a member of enum" – Vagish Aug 25 '17 at 08:05
  • @Vagish You're right - my terminology is a bit off. "Value" would be a better word. I guess you don't normally remember the type of the variable. I just wanted a reference to the enum name, to make it more clear that the value "F164" was not just a defined value but a value in an enum. But using the enum name as the variable type in the function arguments is a good approach, I think. – Oystein Aug 25 '17 at 08:14
1

enums in C aren't classes like they are in Java. In general, you can't qualify the name of an enum with its type. That's why this fails:

typedef enum {
  F227 = 0x40 } BOARD_TYPE;

typedef enum {
  F227 = 0x40 } BOARD_TYPE2;

It's a little ugly, but I think the only way to get around this problem is to use the type in the name:

typedef enum {
  BOARD_TYPE_F227 = 0x40 } BOARD_TYPE;
alk
  • 69,737
  • 10
  • 105
  • 255
Kevin Boone
  • 4,092
  • 1
  • 11
  • 15
0

Just use the enumeration value. Additionally you can use the enum-type as function parameter.

void do_work(BOARD_TYPE board_type) {
    if (board_type == F164) {
        // Do stuff...
    }
}
Batuu
  • 595
  • 1
  • 8
  • 22
  • 1
    As I understand it, this isn't a question about validity, but about readability. There's no _need_ to qualify the enum name with its type, but on occasions it would be nice to be able to do so, to make the code more expressive. I don't think this can be done in C. – Kevin Boone Aug 25 '17 at 08:06
  • True, but there is no explizit type for enums, so you can get an implizit type cast warning (depends on compiler an warning level settings). Or you do if ((BOARD_TYPE)board_type == F164). – Batuu Aug 25 '17 at 08:10