Is the behavior of subtracting two NULL pointers defined?

前端 未结 4 1022
死守一世寂寞
死守一世寂寞 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:42

    Edit: This answer is only valid for C, I didn't see the C++ tag when I answered.

    No, pointer arithmetic is only allowed for pointers that point within the same object. Since by definition of the C standard null pointers don't point to any object, this is undefined behavior.

    (Although, I'd guess that any reasonable compiler will return just 0 on it, but who knows.)

提交回复
热议问题