User manual

mikroPascal PRO for PIC32
MikroElektronika
209
j := 5; // assign value 10 to variable; j is at the address 0x003A
ptr1 := @i; // ptr1 is pointer to byte, pointing to i
ptr2 := @j; // ptr2 is a pointer pointing to j
x := ptr1^ + ptr2^; // result is equal to the sum of the values pointed to; x = 5
end.
Pointer Subtraction
Similar to addition, you can use Dec to subtract an integral value from a pointer.
If a pointer is declared to point to type, subtracting an integral value n from the the pointer decrements the pointer value
by n * sizeof(type) as long as the pointer remains within the legal range (rst element to one beyond the last
element). If type has a size of 10 bytes, then subtracting 5 from a pointer to type pushes back the pointer 50 bytes in
memory.
For example:
var
a : array[10] of byte; // array a containing 10 elements of type byte
ptr : ^byte; // pointer to byte
begin
ptr := @a[6]; // ptr is pointer to byte, pointing to a[6]
ptr := ptr - 3; // ptr-3 is a pointer pointing to a[3]
ptr^ := 6; // a[3] now equals 6
Dec(ptr); // ptr now points to the previous element of array a: a[2]
end.
Also, you may subtract two pointers. The difference will be equal to the distance between two pointed addresses, and
is calculated regarding to the type which the pointer points to.
For example:
var
i, j, x : byte; // variables
ptr1 : ^byte; // pointers to byte
ptr2 : ^byte;
begin
i := 10; // assign value 10 to variable; i is at the address 0x0039
j := 5; // assign value 5 to variable; j is at the address 0x003A
ptr1 := @i; // ptr1 is a pointer to byte, pointing to i
ptr2 := @j; // ptr2 is a pointer pointing to j
x := ptr2 - ptr1; // result is equal to the distance between the two pointed ad-
dresses; x = 1 (1 byte)
x := ptr1^ - ptr2^; // result is equal to the difference of the values pointed to;
x = 5
end.