Quantcast
Viewing all articles
Browse latest Browse all 22113

Why cast zero to pointer type and access its member work?

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?


Viewing all articles
Browse latest Browse all 22113

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>