Recently, I saw something cool.
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
Basically, I know what this macro does. But there is one thing I can't understand.
Why can we cast 0
into pointer type ( struct ) and access its member ? I have already referenced some similar stuff and 0
seems like a null pointer. So why can we cast a null pointer and then access its member?