<p>What is the best approach to define additional data for typedef enums in C?</p>

<p>Example:</p>

<pre><code>typedef enum {
  kVizsla = 0,
  kTerrier = 3,
  kYellowLab = 10
} DogType;
</code></pre>

<p>Now I would like to define names for each, for example <code>kVizsla</code> should be "vizsla".
I currently use a function that returns a string using a large switch block.</p>

[{'answer_id': 35994, 'author': 'On Freund', 'author_id': 2150, 'author_profile': 'https://Stackoverflow.com/users/2150', 'pm_score': 0, 'selected': False, 'text': "<p>That's kind of an open ended question, but one suggestion would be to use a map with the enum as the key type and the extra information in the value. (If your indices are continuous, unlike the example, you can use a sequence container instead of a map).</p>\n"}, {'answer_id': 35998, 'author': 'dmckee --- ex-moderator kitten', 'author_id': 2509, 'author_profile': 'https://Stackoverflow.com/users/2509', 'pm_score': 1, 'selected': False, 'text': '<p>If your enumerated values are dense enough, you can define an array to hold the strings and just look them up (use NULL for any skipped value and add a special case handler on your lookup routine). </p>\n\n<pre><code>char *DogList[] = {\n  "vizsla", /* element 0 */\n  NULL,\n  NULL,\n  NULL,\n  "terrier", /* element 3 */\n  ...\n};\n</code></pre>\n\n<p>This is inefficient for sparse enumerations.</p>\n\n<p>Even if the enumeration is not dense, you can use an array of structs to hold the mapping.</p>\n\n<pre><code>typedef struct DogMaps {\n  DogType index;\n  char * name;\n} DogMapt;\nDogMapt DogMap[] = {\n  {kVizsla, "vizsla"},\n  {kTerrier, "terrier"},\n  {kYellowLab, "yellow lab"},\n  NULL\n};\n</code></pre>\n\n<p>The second approach is very flexible, but it does mean a search through the mapping every time you need to use the data. For large data sets consider a b-tree or hash instead of an array.</p>\n\n<p>Either method can be generalized to connect more data. In the first use an array of structs, in the second just add more members to the struct.</p>\n\n<p>You will, of course, want to write various handlers to simplify your interaction with these data structures.</p>\n\n<hr>\n\n<p>@Hershi By all means, separate code and data. The above examples are meant to be clear rather than functional. </p>\n\n<p>I blush to admit that I still use whitespace separated flat files for that purpose, rather than the kind of structured input you exhibit, but my production code would read as much of the data from external sources as possible.</p>\n\n<hr>\n\n<p>Wait, I see that you mean code generation. </p>\n\n<p>Sure. Nothing wrong with that.</p>\n\n<p>I suspect, though that the OP was interested in what the generated code should look like...</p>\n'}, {'answer_id': 36075, 'author': 'Hershi', 'author_id': 1596, 'author_profile': 'https://Stackoverflow.com/users/1596', 'pm_score': 3, 'selected': True, 'text': '<p>@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.</p>\n\n<p>The XML would be of the form:</p>\n\n<pre><code>&lt;EnumsDefinition&gt;\n    &lt;Enum name="DogType"&gt;\n        &lt;Value name="Vizsla" value="0" /&gt;\n        &lt;Value name="Terrier" value="3" /&gt;\n        &lt;Value name="YellowLab" value="10" /&gt;\n    &lt;/Enum&gt;\n&lt;/EnumsDefinition&gt;\n</code></pre>\n\n<p>and the resulting code would be something similar to what dmckee suggested in his solution.</p>\n\n<p>For information of how to write such an XSLT try <a href="http://www.w3schools.com/xsl/" rel="nofollow noreferrer">here</a> 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.</p>\n'}, {'answer_id': 39816, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>A perfect fit for <a href="http://www.ddj.com/cpp/184401387" rel="nofollow noreferrer" title="X() macros">X() macros</a>. These types of macros can use the C preprocessor to construct enums and arrays from the same source. You only need to add new data to the #define containing the X() macro.</p>\n\n<p>Your example can be written as follows:</p>\n\n<pre><code>// All dog data goes in this list\n#define XDOGTYPE \\\n  X(kVizsla,0,"vizsla") \\\n  X(kTerrier,3,"terrier") \\\n  X(kYellowLab,10,"yellowlab")\n\n // Dog info\n typedef struct {\n     int val;       // Defined value\n     char * desc;   // Text description\n } DogType;\n\n // Build an array index using the Names\n typedef enum {\n  #define X(Name,Val,Text)     Name,\n   XDOGTYPE\n  #undef X\n  MAXDOGS\n } DogIndex;\n\n // Build a lookup table of values\n DogType Dog[] = {\n  #define X(Name,Val,Text)    {Val,Text},\n   XDOGTYPE\n  #undef X\n };\n\n // Access the values\n for (i=0; i &lt; MAXDOGS; i++)\n    printf("%d: %s\\n",Dog[i].val,Dog[i].desc);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/35973', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3740/']