What is the best approach to define additional data for typedef enums in C?
Example:
typedef enum { kVizsla = 0, kTerrier = 3, kYellowLab = 10 } DogType;
Now I would like to define names for each, for example kVizsla should be ‘vizsla’. I currently use a function that returns a string using a large switch block.
@dmckee: I think the suggested solution is good, but for simple data (e.g. if only the name is needed) it could be augmented with auto-generated code. While there are lots of ways to auto-generate code, for something as simple as this I believe you could write a simple XSLT that takes in an XML representation of the enum and outputs the code file.
The XML would be of the form:
and the resulting code would be something similar to what dmckee suggested in his solution.
For information of how to write such an XSLT try here or just search it up in google and find a tutorial that fits. Writing XSLT is not much fun IMO, but it’s not that bad either, at least for relatively simple tasks such as these.