Could I ever want to access the address zero?

前端 未结 17 3017
花落未央
花落未央 2020-11-29 00:38

The constant 0 is used as the null pointer in C and C++. But as in the question \"Pointer to a specific fixed address\" there seems to be some possible use of assigning fixe

17条回答
  •  孤独总比滥情好
    2020-11-29 01:23

    The compiler takes care of this for you (comp.lang.c FAQ):

    If a machine uses a nonzero bit pattern for null pointers, it is the compiler's responsibility to generate it when the programmer requests, by writing "0" or "NULL," a null pointer. Therefore, #defining NULL as 0 on a machine for which internal null pointers are nonzero is as valid as on any other, because the compiler must (and can) still generate the machine's correct null pointers in response to unadorned 0's seen in pointer contexts.

    You can get to address zero by referencing zero from a non-pointer context.

提交回复
热议问题