title: How do one use `offsetof` to access a field in a standard conforming way?
body: <p>Let's suppose I have a struct and extract the offset to a member:</p>
  
  <pre><code>struct A {
      int x;
  };
  
  size_t xoff = offsetof(A, x);
  </code></pre>
  
  <p>how can I, given a pointer to <code>struct A</code> extract the member in a standard conforming way? Assuming of course that we have a correct <code>struct A*</code> and a correct offset. One attempt would be to do something like:</p>
  
  <pre><code>int getint(struct A* base, size_t off) {
      return *(int*)((char*)base + off); 
  }
  </code></pre>
  
  <p>Which probably will work, but note for example that pointer arithmetics only seem to be defined in the standard if the pointers are pointers of the same array (or one past the end), this need not be the case. So technically that construct would seem to rely on undefined behaviour. </p>
  
  <p>Another approach would be</p>
  
  <pre><code>int getint(struct A* base, size_t off) {
      return *(int*)((uintptr_t)base + off);
  }
  </code></pre>
  
  <p>which also probably would work, but note that <code>intptr_t</code> is not required to exist and as far as I know arithmetics on <code>intptr_t</code> doesn't need to yield the correct result (for example I recall some CPU has the capability to handle non-byte aligned addresses which would suggest that <code>intptr_t</code> increases in steps of 8 for each <code>char</code> in an array).</p>
  
  <p>It looks like there's something forgotten in the standard (or something I've missed).</p>