c - Why is this code able to access a null pointer without causing a crash? -


i have come across following piece of code, finds offset of member within structure. not able make out why not crash, though tries derefer null pointer

struct a_ {     int a;     int b; };  int main() {     int offset = &(((struct a_ *) 0x0)->b);     printf ("offset of b = %x\n", offset); }   output = 4 

however not able make out why not crash, though tries derefer null pointer.

you forming invalid address undefined behavior there no dereference of null pointer because of & operator cancels dereference.


Comments

Popular posts from this blog

python - Installing PyDev in eclipse is failed -

PHP OOP-based login system -

c# - Nested Internal Class with Readonly Hashtable throws Null ref exception.. on assignment -