Is the behavior of subtracting two NULL pointers defined?

前端 未结 4 1016
死守一世寂寞
死守一世寂寞 2020-12-01 10:07

Is the difference of two non-void pointer variables defined (per C99 and/or C++98) if they are both NULL valued?

For instance, say I have a buffer struc

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 10:51

    I found this in the C++ standard (5.7 [expr.add] / 7):

    If two pointers [...] both are null, and the two pointers are subtracted, the result compares equal to the value 0 converted to the type std::ptrdiff_t

    As others have said, C99 requires addition/subtraction between 2 pointers be of the same array object. NULL does not point to a valid object which is why you cannot use it in subtraction.

提交回复
热议问题