System Ptr (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code demostrates the use of Ptr function. The example makes use of Ptr to simulate pointer arithmetics.

Code

function FastUpperCase(const S: String): String;
var
  C: ^Char;
  I: Integer;
begin
  { Get the text in the edit box. }
  Result := S;

  { Ensure the string reference stored in Result is unique,
    otherwise you may have to change the original string also.
    No copy-on-write is made if you are accesing the character
    directly.
  }
  UniqueString(Result);

  { Find the address of the first char in the string. }
  C := Addr(Result[1]);

  for I := 0 to Length(Result) - 1 do
  begin
    { Uppercase the character. }
    C^ := UpCase(C^);

    { Move to the next character. }
    C := Ptr(Integer(C) + SizeOf(Char));
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  { Uppercase the string in the edit box. }
  Edit1.Text := FastUpperCase(Edit1.Text);
end;

Uses