Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework


Using Pointers, Arrays, and String Constants



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə87/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   83   84   85   86   87   88   89   90   ...   294
DelphiLanguageGuide

Using Pointers, Arrays, and String Constants
To manipulate null-terminated strings, it is often necessary to use pointers. (See Pointers and pointer types.) String
constants are assignment-compatible with the PChar and PWideChar types, which represent pointers to null-
terminated arrays of Char and WideChar values. For example,
var P: PChar;
  ...                     
P := 'Hello world!'
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!';
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,
73


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.
The 
StrUpper
 function illustrates the use of pointer indexing to iterate through a null-terminated string:
function StrUpper(Dest, Source: PChar; MaxLen: Integer): PChar;
var
  I: Integer;
begin
  I := 0;
  while (I < MaxLen) and (Source[I] <> #0) do
  begin
    Dest[I] := UpCase(Source[I]);
    Inc(I);
  end;
  Dest[I] := #0;
  Result := Dest;
end;

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   83   84   85   86   87   88   89   90   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin