clanguage-lawyerpointer-arithmeticnull-pointer

Three questions: Is NULL - NULL defined? Is (uintptr_t)NULL - (uintptr_t)NULL defined?


  1. Is NULL - NULL defined.?

  2. Is (char *)NULL - (char *)NULL defined.?

  3. Is (uintptr_t)NULL - (uintptr_t)NULL defined?

I know that it works in all used by me implementations. But how does it look like from the standard point of view? I cant find the clear answer.

Edit: From the dupe I assume that the question one answer is: YES.

What about the second and third questions?


Solution

  • The C Standard documents NULL as being defined as a macro which expands to an implementation-defined null pointer constant

    Depending on the actual definition, the expression NULL - NULL may have a defined value or not. For example:

    The second question: are (char*)NULL - (char*)NULL or (uintptr_t)NULL - (uintptr_t)NULL defined?. These expressions are no longer constraint violations: