Quick start manual

Data types, variables, and constants
5-27
Pointers and pointer types
You can also use the file of ... construction directly in a variable declaration. For
example,
var List1: file of PhoneEntry;
The word file by itself indicates an untyped file:
var DataFile: file;
For more information, see “Untyped files” on page 8-4.
Files are not allowed in arrays or records.
Pointers and pointer types
A pointer is a variable that denotes a memory address. When a pointer holds the
address of another variable, we say that it points to the location of that variable in
memory or to the data stored there. In the case of an array or other structured type, a
pointer holds the address of the first element in the structure. If that address is
already taken, then the pointer holds the address to the first element.
Pointers are typed to indicate the kind of data stored at the addresses they hold. The
general-purpose Pointer type can represent a pointer to any data, while more
specialized pointer types reference only specific types of data. Pointers occupy four
bytes of memory.
Overview of pointers
To see how pointers work, look at the following example.
1 var
2 X, Y: Integer; // X and Y are Integer variables
3 P: ^Integer; // P points to an Integer
4 begin
5 X := 17; // assign a value to X
6 P := @X; // assign the address of X to P
7 Y := P^; // dereference P; assign the result to Y
8 end;
Line 2 declares X and Y as variables of type Integer. Line 3 declares P as a pointer to
an Integer value; this means that P can point to the location of X or Y. Line 5 assigns a
value to X, and line 6 assigns the address of X (denoted by @X) to P. Finally, line 7
retrieves the value at the location pointed to by P (denoted by ^P) and assigns it to Y.
After this code executes, X and Y have the same value, namely 17.
The @ operator, which we have used here to take the address of a variable, also
operates on functions and procedures. For more information, see “The @ operator”
on page 4-12 and “Procedural types in statements and expressions” on page 5-32.