Quick start manual
Data types, variables, and constants
5-29
Pointers and pointer types
Pointer types
You can declare a pointer to any type, using the syntax
type pointerTypeName = ^type
When you define a record or other data type, it’s a common practice also to define a
pointer to that type. This makes it easy to manipulate instances of the type without
copying large blocks of memory.
Standard pointer types exist for many purposes. The most versatile is Pointer, which
can point to data of any kind. But a Pointer variable cannot be dereferenced; placing
the ^ symbol after a Pointer variable causes a compilation error. To access the data
referenced by a Pointer variable, first cast it to another pointer type and then
dereference it.
Character pointers
The fundamental types PAnsiChar and PWideChar represent pointers to AnsiChar and
WideChar values, respectively. The generic PChar represents a pointer to a Char (that
is, in its current implementation, to an AnsiChar). These character pointers are used to
manipulate null-terminated strings. (See “Working with null-terminated strings” on
page 5-14.)
Type-checked pointers
The $T compiler directive controls the types of pointer values generated by the @
operator. This directive takes the form of:
{$T+} or {$T-}
In the {$T-} state, the result type of the @ operator is always an untyped pointer that
is compatible with all other pointer types. When @ is applied to a variable reference
in the {$T+} state, the type of the result is ^T, where T is compatible only with
pointers to the type of the variable.
Other standard pointer types
The System and SysUtils units declare many standard pointer types that are
commonly used.
Table 5.6 Selected pointer types declared in System and SysUtils
Pointer type Points to variables of type
PAnsiString, PString AnsiString
PByteArray TByteArray (declared in SysUtils). Used to typecast dynamically
allocated memory for array access.
PCurrency, PDouble,
PExtended, PSingle
Currency, Double, Extended, Single
PInteger Integer
POleVariant OleVariant