<p>I'm looking for a piece of code that can tell me the offset of a field within a structure without allocating an instance of the structure.</p>

<p>IE: given</p>

<pre><code>struct mstct {
    int myfield;
    int myfield2;
};
</code></pre>

<p>I could write:</p>

<pre><code>mstct thing;
printf("offset %lu\n", (unsigned long)(&amp;thing.myfield2 - &amp;thing));
</code></pre>

<p>And get <code>offset 4</code> for the output. How can I do it without that <code>mstct thing</code> declaration/allocating one?</p>

<p>I know that <code>&amp;&lt;struct&gt;</code> does not always point at the first byte of the first field of the structure, I can account for that later.</p>

How about the standard offsetof() macro (in stddef.h)?
Edit: for people who might not have the offsetof() macro available for some reason, you can get the effect using something like:
```
```
#define OFFSETOF(type, field)    ((unsigned long) &(((type *) 0)->field))
```
```