Earlz
Earlz

Reputation: 63905

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?

Upvotes: 2

Views: 6960

Answers (2)

David Heffernan
David Heffernan

Reputation: 613461

Like this:

type
  PMyType = ^TMyType;

procedure Foo(bar: Integer);
var
  ptr: PMyType;
begin
  ptr := PMyType(bar);
end;

Upvotes: 7

TridenT
TridenT

Reputation: 4909

You must cast it explicitely with the new type:

  type PMyType = ^TMyType;

  ptr := PMyType(bar);

or

  ptr := pointer(bar);

Upvotes: 3

Related Questions