Node:Pointer Types, Next:, Previous:Set Types, Up:Type Definition Possibilities



Pointer Types

pointer_type_identifier = ^type_identifier;

A pointer of the type pointer_type_identifier holds the address at which data of the type type_identifier is situated. Unlike other identifier declarations, where all identifiers in definition part have to be declared before, in a pointer type declaration type_identifier may be declared after pointer_type_identifier. The data pointed to is accessed by pointer_type_variable^. To mark an unassigned pointer, the nil constant (which stands for "not in list") has to be assigned to it, which is compatible with all pointer types.

type
  ItselfFoo = ^ItselfFoo;  { possible but mostly senseless }

  PInt      = ^Integer;    { Pointer to an Integer }

  PNode     = ^TNode;      { Linked list }
  TNode     = record
    Key     : Integer;
    NextNode: PNode;
  end;

var
  Foo, Bar: PInt;

begin
  Foo := Bar;  { Modify address which foo is holding }
  Foo^ := 5;   { Access data foo is pointing to }
end.

GPC also suports pointers to procedures or function and calls through them. This is a non-standard feature.

program ProcPtrDemo (Output);

type
  ProcPtr = ^procedure (i: Integer);

var
  PVar: ProcPtr;

procedure WriteInt (i: Integer);
begin
  WriteLn ('Integer: ', i : 1)
end;

begin
  { Let PVar point to function WriteInt }
  PVar := @WriteInt;

  { Call the function by dereferencing the function pointer }
  PVar^ (12345)
end.

See also: Pointer (Intrinsic).