Mikroelektronika MIKROE-442 데이터 시트
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
211
Note: Comparing pointers pointing to different objects/arrays can be performed at programmer’s own responsibility — a
precise overview of data’s physical storage is required.
Pointer Addition
You can use
Inc
to add an integral value to a pointer. The result of addition is defined only if the pointer points to an
element of an array and if the result is a pointer pointing to the same array (or one element beyond it).
If a pointer is declared to point to
type
, adding an integral value n to the pointer increments the pointer value by
n *
sizeof(type)
as long as the pointer remains within the legal range (first element to one beyond the last element). If
type
has a size of 10 bytes, then adding 5 to a pointer to
type
advances the pointer 50 bytes in memory.
For example:
dim
a as byte[10] ‘ array a containing 10 elements of type byte
ptr as ^byte ‘ pointer to byte
main:
ptr = @a[0] ‘ ptr is pointer to byte, pointing to a[0]
ptr = ptr + 3 ‘ ptr+3 is a pointer pointing to a[3]
ptr^ = 6 ‘ a[3] now equals 6
Inc(ptr) ‘ ptr now points to the next element of array a: a[4]
end.
Also, you may sum values pointed to by pointers.
For example:
dim
i, j, x as byte ‘ variables
ptr1 as ^byte ‘ pointers to byte
ptr2 as ^byte
main
i = 10 ‘ assign value 10 to variable; i is at the address 0x0038
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 (first 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.