Wednesday 7 July 2010

Pointer

Pointer is a variable that points toward a memory address. So a pointer is a data address. The data is somewhere else. Below picture will show you the relationship between a pointer and a data pointed by a pointer.

To declare a pointer, use the below statement :

Type

   PointerType : ^Type;

In this case, type can be an integer, a real, an array or record. And you can see the example code of using a pointer :


program Project1;
   {$APPTYPE CONSOLE}
uses
   SysUtils;

type
   PointerType = ^integer;

var
   MyPointer : PointerType;
   Data : integer;

begin
  Data := 100;
  MyPointer := @Data;
  writeln('Data pointed toward by the pointer is ', MyPointer^);
  Data := 123;
  writeln('Data pointed toward by the pointer is ', MyPointer^);
  readln;
end.


Disposing pointer allocation

To free the memory that is used by a pointer, you can use dispose statement.

  Dispose(pointer);

Example :

program Project1;
   {$APPTYPE CONSOLE}
uses
   SysUtils;

var
   MyPointer : ^integer;

begin
  New(MyPointer); // allocate memory
  MyPointer^ := 100;
  writeln('Vallue pointed toward by MyPointer is ',MyPointer^);
  Dispose(MyPointer); // free memory allocation
  readln;
end.


You can download the complete code here.

1 comment:

  1. makin kerenz postingnya, dan makin oke

    terimakasih atas sharingnya dan moga sukses selalu kawan

    ReplyDelete

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.