This gives an offset in bytes of the field b inside the hi struct
((struct hi *)0) is a pointer to a hi structure, starting at address 0 .
(((struct hi *)0)->b) is the field b above structure
& (((struct hi *)0)->b) is the address of the specified field. Since the structure hi is located at address 0 , this is the offset b inside the structure.
(unsigned int) & (((struct hi *)0)->b) is the conversion of this address from the address type to unsigned int , so it can be used as a number.
You are not actually casting a NULL pointer. You just do pointer arithmetic.
Accessing (((struct hi *)0)->b) will give you a segmentation error because you are trying to access a forbidden memory location.
Using & (((struct hi *)0)->b) does not give you a segmentation error, because you only accept the address of this forbidden memory location, but you are not trying to access the specified location.
Nathan fellman
source share