Why NULL is not predefined by the compiler

前端 未结 4 1711
广开言路
广开言路 2021-01-13 06:33

This issue bothered me for a while. I never saw a different definition of NULL, it\'s always

#define NULL  ((void *) 0)

is there any archi

4条回答
  •  佛祖请我去吃肉
    2021-01-13 06:57

    C 2011 Standard, online draft

    6.3.2.3 Pointers
    ...
    3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
    66) The macro NULL is defined in (and other headers) as a null pointer constant; see 7.19.

    The macro NULL is always defined as a zero-valued constant expression; it can be a naked 0, or 0 cast to void *, or some other integral expression that evaluates to 0. As far as your source code is concerned, NULL will always evaluate to 0.

    Once the code has been translated, any occurrence of the null pointer constant (0, NULL, etc.) will be replaced with whatever the underlying architecture uses for a null pointer, which may or may not be 0-valued.

提交回复
热议问题