Technically, numPtrA
doesn't point to an array nor even a singular object, and therefore adding anything other than 0 to it results in undefined behaviour in C++ because pointer arithmetic is only defined for pointers within bounds of arrays and objects.
Here is the quote from latest C++ standard draft:
[expr.add]
When an expression J that has integral type is added to or subtracted from an expression P of pointer type, the result has the type of P.
- If P evaluates to a null pointer value and J evaluates to 0, the result is a null pointer value. [does not apply]
- Otherwise, if P points to an array element i of an array object x with n elements ... [does not apply] (note: pointers to objects can be considered as arrays of 1 element for purposes of this rule)
- Otherwise, the behavior is undefined.
Besides that technicality, 0x prefix denotes hexadecimal base. 5 * 4 = 20 = 0x14
and 0x20 = 32 != 20
.
That said, the value of null pointer is not necessarily 0 (even though the literal 0 is always a null pointer literal), so the expectation is not portable to some systems where that isn't the case. This is typical on embedded systems where address 0 is wanted for actual storage.
Furthermore, printf
specifier %p
requires the argument to be of type void*
while you pass an int*
as argument. As a result of violating this constraint, the behaviour of the program is undefined.