Pointer Arithmetic

Pointer arithmetic in the mikroPascal PRO for PIC is limited to:

Assignment and Comparison

The simple assignment operator (=) can be used to assign value of one pointer to another if they are of the same type.

Assigning the integer constant 0 to a pointer assigns a null pointer value to it.

Two pointers pointing to the same array may be compared by using relational operators =, <>, <, <=, >, and >=. Results of these operations are the same as if they were used on subscript values of array elements in question:

var ptr1 : ^byte;
    ptr2 : ^byte;
    a : array[10] of byte;    // array a containing 10 elements of type byte

begin           
  ptr1 := @a[4];
  ptr2 := @a[2];

  if (ptr1 = ptr2) then ...   // won't be executed as 4 is not equal to 2 
  if (ptr1 > ptr2) then ...   // will be executed as 4 is greater than 2 

  if (ptr1^ = ptr2^) then ... // if the value pointed to by ptr1 is equal to the value pointed to by ptr2 ...
  if (ptr1^ > ptr2^) then ... // if the value pointed to by ptr1 is greater to the value pointed to by ptr2 ...
end.
  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:

var
  a : array[10] of byte;    // array a containing 10 elements of type byte
  ptr : ^byte;              // pointer to byte
  	
begin
  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:

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 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.

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 addresses; x = 1 (1 byte)  
  x := ptr1^ - ptr2^;   // result is equal to the difference of the values pointed to; x = 5
end.
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code