title: Iterate through enums in C?
body: <p>let's say I have a couple of hardware registers which are defined within an enum:</p>
  <pre><code>typedef enum registers
  {
      REG1 = 0,
      REG2 = 1,
      REG3 = 2,
      REG4 = 4,
      REG5 = 6,
      REG6 = 8,
      
      REG_MAX,
      
  }
  </code></pre>
  <p>I have default values for these registers(actually not decided in which way to define them, #define, array or enum...):</p>
  <pre><code>// This is just conceptual, not really an enum, array or #define :)
  typedef enum values
  {
      VALUE_REG1 = A,
      VALUE_REG2 = B,
      VALUE_REG3 = C,
      VALUE_REG4 = 53,
      VALUE_REG5 = 88,
      VALUE_REG6 = 24,
      
      MAX_NUM_REG_VALUES    
  }
  </code></pre>
  <p>I have a function which can read the hardware registers:</p>
  <pre><code>uint8 readRegs(uint8 regaddr);
  </code></pre>
  <p>Now I would like to loop through <code>registers</code>, and on each element call the <code>readRegs()</code> function and compare it to the enum <code>regvalues</code>. It looks like:</p>
  <pre><code>registers regs;
  reg_values values;
  uint8 readfromregs;
  
  for (regs = REG1; regs &lt;= REG_MAX; regs++)
  {
      readfromregs = readRegs(regs);
      for (values = VALUE_REG1; reg_values &lt;= MAX_NUM_REG_VALUES; reg_values++)
      {
          if (readfromregs != values)
          {
               // Value not correct
          }
          else
          {
               // value correct
          }
      }
  }
  </code></pre>
  <p>This will not work because it's not possible to iterate in enum in this way. Does anybody have a better solution? How to define the construct <code>enum reg_values</code>?
  The <em>enum registers</em> must be fix (this can not be changes to array).</p>