I have been searching very long and hard (links at the very end) for an explanation of the implementation of the offsetof MACRO :
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Particularly, the dereferencing of NULL to obtain the offset of the member in the structure. Many articles gloss over the reason by saying that the NULL pointer is actually never really dereferenced, but that doesn't make sense to me.
Here are some of the links I have tried understanding:
- http://www.viva64.com/en/b/0301/
- http://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro
- http://www.geeksforgeeks.org/the-offsetof-macro/
- How does the C offsetof macro work?
- Does dereference a NULL pointer guarantee to crash a program in C/C++?
What I'm looking for and trying to understand is a step by step , broken down understanding of how the compiler interprets the MACRO definition, which will eventually explain how a NULL pointer is not actually being dereferenced.
EDIT: Even though other questions answered my query, they dindt make sense to me, as pointed out in the original post. The answer by @dasblinkenlight sheds light on the exact problem I had with ansers to other questions i.e how is it that we're not actually dereferencing the pointer .
a
is a pointer to address 1234, andb
is a member ofa
at offset 12, then what is&a->b
? Now, ifa
points to address 0, what is&a->b
? How did you work that out in your head without dereferencinga
? – Adhern