[fpc-pascal] Pointer question

Hairy Pixels genericptr at gmail.com
Thu Aug 10 00:06:57 CEST 2023



> On Aug 9, 2023, at 2:54 PM, Tomas Hajny via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
>> 
>> 1) what does "i := x - x;" do and what is it's purpose and why doesn't "x + x" work the same?
> 
> Pointer subtraction is a reverse operation to adding a number to a pointer; the result of the subtraction is the offset between the two pointers.

Is that different than decrementing the pointer?

What would an offset between two pointers be used for? Is the offset even a memory address or just a number of bytes? If you had an example usage that would be interesting to see.

> 
>> 2) I've used pointer equality of course but what does "x > p" do and what is its purpose?
> 
> Relative position between the two locations in memory? As an example, it may be used to check whether a particular pointer points to a location between the start and the end of an allocated memory block (obviously, you'd need two comparisons for that).

I guess that makes sense if a memory address with a higher value is always after an address with a lower value. Is that assumption always true?

Regards,
Ryan Joseph



More information about the fpc-pascal mailing list