Easy way to use variables of enum types as string in C?

前端 未结 19 2047
太阳男子
太阳男子 2020-11-22 08:47

Here\'s what I am trying to do:

typedef enum { ONE, TWO, THREE } Numbers;

I am trying to write a function that would do a switch case sim

19条回答
  •  忘了有多久
    2020-11-22 09:14

    Here is a solution using macros with the following features:

    1. only write each value of the enum once, so there are no double lists to maintain

    2. don't keep the enum values in a separate file that is later #included, so I can write it wherever I want

    3. don't replace the enum itself, I still want to have the enum type defined, but in addition to it I want to be able to map every enum name to the corresponding string (to not affect legacy code)

    4. the searching should be fast, so preferably no switch-case, for those huge enums

    https://stackoverflow.com/a/20134475/1812866

提交回复
热议问题