title: Random Member Access in C
body: <p>Suppose a struct like:</p>
  <pre><code>struct node{
      int a;
      int b;
      char c;
      struct node* next;
  }
  </code></pre>
  <p>And a function which iterates through a singly linked list with a tmp pointer;</p>
  <pre><code>void getMember(char* member_name){
      While(tmp != NULL){
  
      //here, I'd like to access a user defined member passed into the function
      // something like printf(&quot;%d\t&quot;, tmp[member_name]);
  
         tmp = tmp-&gt;next;
      }
  }
  </code></pre>
  <p>I tried using pointer arithmetic to access it via an offset, like:</p>
  <pre><code>//suppose member_name is instead an offset of size, like sizeof(int), rather than the char* shown above;
  printf(&quot;%d\t&quot;,*(&amp;(tmp)+member_name);
  </code></pre>
  <p>But the arithmetic isn't guaranteed in a struct like it is an array because of padding. Aside from turning on packing, which I think would make the arithmetic work, how would one implement random access to members of a struct in C? Can it be achieved with this sort of data structure? Or is using something else necessary?</p>