pointers c5
TRANSCRIPT
Pascal Programming Language
Omar ElSabek & Fayez GhazzawiIT Engineering3th year – UNKNOWN Department
programming II
It’s very important to mention the difference between Value Type Variables & Reference Type Variables
1. Dealing with Value Type Variables means that the variables of my program are reserved in the memory “RAM” so the compiler search for an empty box for the variable
2. Dealing with Reference Type Variables means that we reserve the empty boxes for my variables dynamically“RUN TIME” but we reserve references before
So the POINTER is:
A type which can reserve a reference in the memory “RAM”this type can contain many data with a type (integer ,real ,boolean ,…..)this reference has the SAME type of the data which it refers to
1. Better Usage for the memorybecause sometimes we don’t know the number of variables we want in our program so we define them dynamically
2. Setting up new Data Bases depending on ideas of Pointersbecause we use pointers to build so many complex structures
• We can define a pointer this way Var
PointerName : ^(PointerType);
Juuuuust like Var
I : ^integer;
this means that we defined a reference refers to an integer box
• New(I);This means that we define a variable from the referenceand Don’t Forget to use New() before using the pointer
• Dispose(I);This means that we free the variable box from the memory
• It’s illegal to do do this I := 5; but why ???
• To do it right we write: I^ := 5;
• So we use ‘^’ to access the data of the reference
• To initialize a reference we write I := nil;
Program test2;
Var
t : ^integer; a : integer;
Begin
New(t);
a := 10;
t^ := 3;
a := t^;
writeln(a);
End.
Program test3;
Var
t,i,j : ^integer;
Begin
New(t); i := t;
j := i; i^ := 3;
New(i); i^ := 5; j := i;
New(i); i^ := 10;
writeln(t^,i^,j^);
End.