Why subtract null pointer in offsetof()?

前端 未结 2 1956
面向向阳花
面向向阳花 2020-12-18 03:18

Linux\'s stddef.h defines offsetof() as:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

whereas the Wik

2条回答
  •  不知归路
    2020-12-18 04:16

    The standard does not require the NULL pointer to evaluate to the bit pattern 0 but can evaluate to a platform specific value.

    Doing the subtraction guarantees that when converted to an integer value, NULL is 0.

提交回复
热议问题