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 :
{$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.
makin kerenz postingnya, dan makin oke
ReplyDeleteterimakasih atas sharingnya dan moga sukses selalu kawan