title: How can I get/set a struct member by offset
body: <p><em>Ignoring padding/alignment issues</em> and given the following struct, what is best way to get and set the value of member_b without using the member name.</p>
  
  <pre><code>struct mystruct {
      int member_a;
      int member_b;
  }
  struct mystruct *s = malloc(sizeof(struct mystruct));
  </code></pre>
  
  <p>Put another way; How would you express the following in terms of pointers/offsets:</p>
  
  <pre><code>s-&gt;member_b = 3;
  printf("%i",s-&gt;member_b);
  </code></pre>
  
  <p>My guess is to</p>
  
  <ul>
  <li>calculate the offset by finding the sizeof the member_a (<code>int</code>)</li>
  <li>cast the struct to a single word pointer type (<code>char</code>?)</li>
  <li>create an <code>int</code> pointer and set the address (to <code>*charpointer + offset</code>?)</li>
  <li>use my <code>int</code> pointer to set the memory contents</li>
  </ul>
  
  <p>but I get a bit confused about casting to a char type or if something like <code>memset</code> is more apropriate or if generally i'm aproching this totally wrong.</p>
  
  <p>Cheers for any help</p>