Pointers

We have already demonstrated the use of record pointers in program LinkedListDemo. Our demonstration programs in this section are intended to be simple illustrations of syntax.

Program PointerDemo1 is based on an example of an integer pointer in Delphi 7 Help. It shows how to (i) declare a pointer, (ii) assign a value to it and (iii) dereference it to obtain the value of the integer to which it points.

program PointerDemo1;
  {$Apptype Console}
 var
  Int1, Int2 : integer;
  PtrInt: ^integer;     // PtrInt points to an Integer
begin
  Int1 := 42;
  PtrInt := @Int1;   // Assign the address of Int1 to PtrInt.
  Int2 := PtrInt^;   // Dereference PtrInt and assign the result to Int2.
  writeln('Int2: ', Int2);
  readln;
end.  

Program PointerDemo2 shows how you can use pointers to access successive integers in memory, such as integers in an array. The inc procedure takes account of the four-byte size of an integer.

program PointerDemo2;
  {$Apptype Console}
var
  IntArray : array[1..2] of integer = (10, 20);
  Int1, Int2 : integer;
  PtrInt: ^integer;    //PtrInt points to an Integer
begin
  PtrInt := @IntArray; //Assign the address of IntArray to PtrInt.
  Int1 := PtrInt^;     //Dereference PtrInt and assign the result to Int1.
  inc(PtrInt);		 //Increment PtrInt so that it points to the next
                      //integer in memory.
  Int2 := PtrInt^;     //Dereference the pointer.
  writeln('Int1: ', Int1, '  Int2: ', Int2);
  readln;
end.  

The main purpose of Program PointerDemo3 is to demonstrate the syntax of record pointers. We have noticed that the Delphi compiler ‘forgives’ omissions of pointer dereferences. We tested this in Program PointerDemo3 with the instruction writeln(FirstRec.Surname) instead of writeln(FirstRec^.Surname). When the program, without the {$Mode Delphi} compiler directive, is compiled by Lazarus it causes the following error messages.

Error messages

Error messages

program PointerDemo3;
  {$Mode Delphi}//Delete this line to compile in Delphi.  
uses
  SysUtils;
type
  PtrRec = ^TRec;
  TRec = record
    Surname : string[15];
    Score : integer;
    Next : PtrRec;
  end;
var
  FirstRec : PtrRec;
begin
  //Assign a new record of type TRec to FirstRec.
  FirstRec := new(PtrRec);
  FirstRec^.Surname := 'Sugar';
  FirstRec^.Score := 20;
  writeln('Name: ', FirstRec.Surname);
  writeln('Score: ', FirstRec^.Score);
  dispose(FirstRec);
  readln;
end.  

Program PointerDemo4 confirms that the inc procedure is 'smart'. It automatically takes account of the record size and increments the pointer so as to access the next record in memory.

program PointerDemo4;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
type
  TRec = record
    Surname : string[15];
    Score : integer;
  end;
  PtrRec = ^TRec;
var
  TempRec : PtrRec;
  RecArray : array [0..1] of TRec;

begin
  RecArray[0].Surname := 'Sugar';
  RecArray[0].Score := 20;
  RecArray[1].Surname := 'Gates';
  RecArray[1].Score := 25;
  TempRec := @RecArray;  // or TempRec := addr(RecArray);
  writeln('Name : ', TempRec^.Surname, '     Score: ', TempRec^.Score);
  inc(TempRec);
  writeln('Name : ', TempRec^.Surname, '     Score: ', TempRec^.Score);
  readln;
end.  

SDL identifiers beginning with P are pointers. For example, PSDL_Surface points to a surface of type TSDL_Surface.

Programming - a skill for life!

Topics you need to understand before programming with SDL