title: How to access members of a `struct' according to a variable integer in C?
body: <p>Suppose I have this <code>struct</code> (which incidentally contain bit-fields, but you shouldn't care):</p>
  
  <pre><code>struct Element {
      unsigned int a1 : 1;
      unsigned int a2 : 1;
      ...
      unsigned int an : 1;
  };
  </code></pre>
  
  <p>and I want to access the <i>i'th</i> member in a convenient way. Let's examine a retrieval solution.<br>
  I came up with this function:  </p>
  
  <pre><code>int getval(struct Element *ep, int n)
  {
      int val;
      switch(n) { 
           case 1: val = ep-&gt;a1; break;
           case 2: val = ep-&gt;a2; break;
           ...
           case n: val = ep-&gt;an; break;
      }
      return val;
  }
  </code></pre>
  
  <p>But I suspect that there is a much simpler solution. Something like array accessing style, maybe.</p>
  
  <p>I tried to do something like that:  </p>
  
  <pre><code> #define getval(s,n)   s.a##n
  </code></pre>
  
  <p>But expectedly it doesn't work.<br>
  Is there a nicer solution?</p>