Positive or negative is not a meaningful facet of pointer type. They pertain to signed integer including signed char, short, int etc.
People talk about negative pointer mostly in a situation that treats pointer's machine representation as an integer type. e.g. reinterpret_cast<intptr_t>(ptr)
. In this case, they are actually talking about the cast integer, not the pointer itself.
In some scenario I think pointer is inherently unsigned, we talk about address in terms below or above. 0xFFFF.FFFF
is above 0x0AAAA.0000
, which is intuitively for human beings. Although 0xFFFF.FFFF
is actually a "negative" while 0x0AAA.0000
is positive.
But in other scenarios such as pointer subtraction (ptr1 - ptr2)
that results in a signed value whose type is ptrdiff_t
, it's inconsistent when you compare with integer's subtraction, signed_int_a - signed_int_b
results in a signed int type, unsigned_int_a - unsigned_int_b
produces an unsigned type. But for pointer subtraction, it produces a signed type, because the semantic is the distance between two pointers, the unit is number of elements.
In summary I suggest treating pointer type as standalone type, every type has it's set of operation on it. For pointers (excluding function pointer, member function pointer, and void *
):
- List item
+
, +=
ptr + any_integer_type
-
, -=
ptr - any_integer_type
ptr1 - ptr2
++
both prefix and postfix
--
both prefix and postfix
Note there are no / * %
operations for pointer. That's also supported that pointer should be treated as a standalone type, instead of "A type similar to int" or "A type whose underlying type is int so it should looks like int".