Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi -- Casting an integer to a typed pointer?

I'm having some trouble with the syntax of Delphi.

I have a record:

type
  TMyType = record
    ....
  end;

and a procedure:

procedure Foo(bar:Integer);
var
  ptr : ^TMyType
begin
  ptr := bar //how to do this?
end;

How do I properly cast an integer to a pointer of TMyType?

like image 647
Earlz Avatar asked Jun 21 '26 10:06

Earlz


2 Answers

Like this:

type
  PMyType = ^TMyType;

procedure Foo(bar: Integer);
var
  ptr: PMyType;
begin
  ptr := PMyType(bar);
end;
like image 104
David Heffernan Avatar answered Jun 23 '26 19:06

David Heffernan


You must cast it explicitely with the new type:

  type PMyType = ^TMyType;

  ptr := PMyType(bar);

or

  ptr := pointer(bar);
like image 38
TridenT Avatar answered Jun 23 '26 18:06

TridenT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!