2

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?

2 Answers 2

7

Like this:

type
  PMyType = ^TMyType;

procedure Foo(bar: Integer);
var
  ptr: PMyType;
begin
  ptr := PMyType(bar);
end;
Sign up to request clarification or add additional context in comments.

10 Comments

Ah I wasn't for sure a new type would be required. I figured it was like C where "typecasts" are purely optional
@Earlz typecasts aren't optional in C. You would need to cast to assign an integer to a pointer variable in C.
Pascal object is a strongly typed language, so casts are mandatory between different types.
@TridenT C is strongly typed too. And in fact C and Delphi are similar in the way that any pointer is assignment compatible with void*/Pointer.
Do note that this will fail on Win64 because there the pointer type is 64-bit and integer is still 32-bit
|
3

You must cast it explicitely with the new type:

  type PMyType = ^TMyType;

  ptr := PMyType(bar);

or

  ptr := pointer(bar);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.