Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

No need to define offsetof, it’s part of <stddef.h>

Still interesting to see the trick behind it tho.




It’s added to the standard because macro version has undefined behavior. (yet it still works on any compiler without a problem)


I was about to ask if it was legal to dereference a null pointer and then take the address of it... I presume it is not, but I'm surprised compilers don't complain at compilation time.


Dereferencing a null pointer and then immediately "undoing" it by taking its address is actually legal, I believe. I think the undefined behavior here is the member access instead of the magic sequence &* which is supposed to cancel out.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: