Ptr addition fine, Ptr subtraction compile error

For the following code

[code]
//building in Xojo2017r1.1 for Win 32 bit
dim p1,p2,p3 as ptr

p1=p2+p3 //compiles fine

p1=p2-p3 //Type mismatch error. Expected Ptr but got Int32[/code]

ptr addition compiles fine, but ptr subtraction throws a compile error.

Is this expected behaviour? If so, why is subtraction different to addition?

Because subtracting a pointer from another pointer returns an integer for the distance or offset.

  • of two unsigned integers gives unsigned one.
    But - may give a signed one. And signed integers seem not to convert back to ptr.

this make sense because the result of - is a delta (an offset between the two addresses) not a Ptr (ie a valid address)

But you cannot add an Integer (a delta or offset) to a Ptr – it will not compile:

Dim p As Ptr Dim i As Integer Dim result As Ptr = p + i
Why is this?

because it’s likely to cause problems?

You can cast to integer, add something and cast to ptr.