#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
very similar to the pretty general definition of the standard offsetof() macro defined in <stddef.h> (in C) or <cstddef> (in C ++).
0 is a null pointer constant. Listing TYPE * yields a null pointer of type TYPE * . Note that the language does not guarantee (or even imply) that the null pointer is set to 0, although this happens very often.
So, (TYPE *)0 is, in fact, the address of an object of type TYPE , located at any address pointed to by a null pointer, and ((TYPE *)0)->ELEMENT)) is a member of the ELEMENT this object.
The & operator takes the address of this ELEMENT member, and the listing converts that address to type size_t .
Now, if the null pointer points to address 0, then a (non-existent) object of type TYPE begins with address 0, and the address of the ELEMENT member of this object is located at address, offset by a certain number of bytes from address 0. Assuming that the transformation defined by the implementation from TYPE * up to size_t , behaves in a straightforward manner (something else that is not guaranteed by the language), the result of the whole expression will be the offset of the ELEMENT member in an object of type TYPE .
It all depends on several undefined or undefined behavior. In most modern systems, a null pointer is implemented as a pointer to address 0, addresses (pointer values) are presented as if they were integers indicating the index of a particular byte in a space of monolithic addressing, and the conversion from a pointer to an integer of the same size simply reinterprets bit. In a system with these characteristics, the OFFSETOF macro is likely to work, and the implementation can choose a similar definition for the standard OFFSETOF macro. (Code that part of the implementation can take advantage of the implementation or undefined, and you do not need to port them.)
On systems that do not have these characteristics, this OFFSETOF macro may not work - and the implementation must use a different method to implement OFFSETOF . Therefore, OFFSETOF is part of the standard library; it cannot be implemented portable, but it can always be somehow implemented for any system. And some implementations use compiler magic, such as gcc __builtin_offsetof .
In practice, it makes no sense to define your own OFFSETOF macro like this, since any corresponding C or C ++ implementation will provide a working OFFSETOF macro in its standard library.