Quick start manual

Data types, variables, and constants
5-15
String types
points P to an area of memory that contains a null-terminated copy of “Hello world!”
This is equivalent to
const TempString: array[0..12] of Char = 'Hello world!'#0;
var P: PChar;
ƒ
P := @TempString[0];
You can also pass string constants to any function that takes value or const
parameters of type PChar or PWideChar—for example StrUpper('Hello world!'). As
with assignments to a PChar, the compiler generates a null-terminated copy of the
string and gives the function a pointer to that copy. Finally, you can initialize PChar
or PWideChar constants with string literals, alone or in a structured type. Examples:
const
Message: PChar = 'Program terminated';
Prompt: PChar = 'Enter values: ';
Digits: array[0..9] of PChar = (
'Zero', 'One', 'Two', 'Three', 'Four',
'Five', 'Six', 'Seven', 'Eight', 'Nine');
Zero-based character arrays are compatible with PChar and PWideChar. When you
use a character array in place of a pointer value, the compiler converts the array to a
pointer constant whose value corresponds to the address of the first element of the
array. For example,
var
MyArray: array[0..32] of Char;
MyPointer: PChar;
begin
MyArray := 'Hello';
MyPointer := MyArray;
SomeProcedure(MyArray);
SomeProcedure(MyPointer);
end;
This code calls SomeProcedure twice with the same value.
A character pointer can be indexed as if it were an array. In the previous example,
MyPointer[0] returns H. The index specifies an offset added to the pointer before it is
dereferenced. (For PWideChar variables, the index is automatically multiplied by
two.) Thus, if P is a character pointer, P[0] is equivalent to P^ and specifies the first
character in the array, P[1] specifies the second character in the array, and so forth;
P[-1] specifies the “character” immediately to the left of P[0]. The compiler performs no
range checking on these indexes.