Pointers

A pointer is a data type which holds a memory address. While a variable accesses that memory address directly, a pointer can be thought of as a reference to that memory address.

To declare a pointer data type, add a carat prefix (^) before type. For example, in order to create a pointer to an integer, write:

^integer;

In order to access data at the pointer’s memory location, add a carat after the variable name. For example, let’s declare variable p which points to a word, and then assign value 5 to the pointed memory location:

var p : ^word;
...
p^ := 5;

A pointer can be assigned to another pointer. However, note that only the address, not the value, is copied. Once you modify the data located at one pointer, the other pointer, when dereferenced, also yields modified data.

Pointers and memory spaces

Pointers can point to data in any available memory space.
Pointers can reside in any available memory space except in program (code) memory space.

var ptr1: ^const byte;                   // ptr1 pointer in data space pointing to a byte in code space
var ptr2: ^const ^volatile sfr byte; rx; // ptr2 is pointer in rx space pointing to a pointer in code space pointing to volatile byte in sfr space
var ptr3: ^data byte; code;              // error, pointers can not be placed in code space

Due to backward compatibility, pointers to program memory space can also be declared within constant declaration block (using keyword const):

program const_ptr;

// constant array will be stored in program memory
const b_array: array[5] of byte = (1,2,3,4,5);

const ptr: ^byte;     // ptr is pointer to program memory space

begin
  ptr   := @b_array;  // ptr now points to b_array[0]
  PORTA := ptr^;
  ptr   := ptr + 3;   // ptr now points to b_array[3]
  PORTA := ptr^;
end.

This leads to equality of the following declarations:

var   ptr1 : ^const byte;  // ptr1 pointer in data space pointing to a byte in code space
const ptr2 : ^byte;        // ptr2 pointer in data space pointing to a byte in code space

Therefore, when declaring a pointer within constant declaration block, const qualifier refers to pointed object, not to pointer itself.

  Notes :

Function Pointers

Function pointers are allowed in mikroPascal PRO for PIC. The example shows how to define and use a function pointer:

Example:

Example demonstrates the usage of function pointers. It is shown how to declare a procedural type, a pointer to function and finally how to call a function via pointer.

 program Example;

 type TMyFunctionType = function (param1, param2: byte; param3: word) : word;  // First, define the procedural type
 var MyPtr: ^TMyFunctionType;                                                  // This is a pointer to previously defined type
     Sample: word;
 
 function Func1(p1, p2: byte; p3: word): word;              // Now, define few functions which will be pointed to. Make sure that parameters match the type definition 
 begin
     result := p1 and p2 or p3;        // return something
 end;
 
 function Func2(abc: byte; def: byte; ghi: word): word;     // Another function of the same kind. Make sure that parameters match the type definition  
 begin
   result := abc * def + ghi;          // return something
 end;
          
 function Func3(first, yellow: byte; monday: word): word;   // Yet another function. Make sure that parameters match the type definition   
 begin
   result := monday - yellow - first;  // return something
 end;
 
 //  main program:
 begin
    MyPtr  := @Func1;                // MyPtr now points to Func1
    Sample := MyPtr^(1, 2, 3);       // Perform function call via pointer, call Func1, the return value is 3
    MyPtr  := @Func2;                // MyPtr now points to Func2
    Sample := MyPtr^(1, 2, 3);       // Perform function call via pointer, call Func2, the return value is 5
    MyPtr  := @Func3;                // MyPtr now points to Func3
    Sample := MyPtr^(1, 2, 3);       // Perform function call via pointer, call Func3, the return value is 0
 end.

Therefore, when declaring a pointer within constant declaration block, const qualifier refers to pointed object, not to pointer itself.

@ Operator

The @ operator constructs a pointer to its operand. The following rules are applied to @:

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