Pointers are collections of elements (nodes) that are records. The difference between arrays of records and pointers is that an array has a predefined size and storage for this size is always available, but a pointer has no fixed size and therefore has no storage available unless the program says to do so.
Pointers are also called dynamic variables, because they are by no means static, they get created and destroyed as the program executes.
General declaration:
|
![]() |
Lets see an example step by step (taken from Koffmann, fourth edition):
type intpointer = ^integer;
var p : intpointer;
begin
new(p);
Now, the memory space is reserved and could be represented by .
Now you wonder why there is a ?. It's because you have not initialized
the variable and it now contains junk.
After the statement p^:= 4; the state changes to .
Now you can output the value of p by using writeln(p^);
Do
not forget the end. at the end of the program.
Important:
As a pointer contains only a memory address, you cannot write p:=
anything, and you cannot display its value directly by using writeln(p);
Returning the memory cell to the Memory:
By using dispose(p) at the end of the program, you can return the memory cell back to the memory. Important is that you dispose a cell which has really been allocated, otherwise you will get into trouble. Furthermore, do not try to use a pointer after you have disposed it, if you really need to then you should allocate space for it, again.
Records with Pointer Fields
Suppose you have to maintain a list of persons as declared in the section
Records.
As you do not now beforehand, how many persons there will be in the list,
declaring an array on maximum size would be very inefficient and bad programming.
Instead you could have a list and append to it. This list could be constructed
with the help of Records with Pointer Fields. For this you should have
a field in the person record, which can point to new persons. This could
be achieved by:
type |
PersonPointer = ^Person; |
Person = record |
age : integer; |
sex : char; |
IDno : integer; |
Link : PersonPointer |
end; |
var |
P,Q,R : PersonPointer; |
Suppose now we write a part of code with following statements:
new(P); |
new(Q); |
P^.age:=20; |
P^.sex:='F'; |
P^.IDno:=9; |
Q^.age:=15; |
Q^.sex:='M'; |
Q^.IDno:=8; |
After the execution of this piece of code, the state of the variables
is as follows:
Here, the symbol originating from the 'Link' parts of the pointers denote that it points nowhere, yet.
Lets continue with the problem:
The line R:=P; is also an assignment statement. Note that you do not(!) need a new before this.
Now, the state of the variables is
:
A last possibility would be to assign the values of Q to R without assigning its memory address to it. This is done by:
R^:=Q^. Now the state of the variables is:
Now, rather than changing the values of a pointer, constructing a list is more important for our case.
With the line : R^.Link:=P; the state becomes
Now, using these concepts and examples, you are able to cnstruct linked lists. For more information about it, see the course CmpE 160.