Re: offsetof



In article <1128074405.066074.296020@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
<junky_fellow@xxxxxxxxxxx> wrote:

>> #define offsetof(type, mem) ((size_t) \
>> ((char *)&((type *)0)->mem - (char *)(type *)0))

>The second member of sustraction would be required on implementation
>where the NULL pointer is *not* represented by all-bits-zero.

The subtraction is required because you want an integer, not a
pointer.

Whether ((type *)0)->mem cast to an integer would give the right
answer depends on how pointer-to-integer conversion works, not just on
the representation of the null pointer.

But the definition is non-portable because ((type *)0)->mem is undefined.

-- Richard
.



Relevant Pages