Delphi--将整数转换为类型化指针?

Delphi--将整数转换为类型化指针?,delphi,pointers,syntax,delphi-7,Delphi,Pointers,Syntax,Delphi 7,我在Delphi的语法方面遇到了一些问题 我有一个记录: type TMyType = record .... end; 和一个程序: procedure Foo(bar:Integer); var ptr : ^TMyType begin ptr := bar //how to do this? end; 如何正确地将整数强制转换为TMyType指针?如下所示: type PMyType = ^TMyType; procedure Foo(bar: Integ

我在Delphi的语法方面遇到了一些问题

我有一个记录:

type
  TMyType = record
    ....
  end;
和一个程序:

procedure Foo(bar:Integer);
var
  ptr : ^TMyType
begin
  ptr := bar //how to do this?
end;
如何正确地将整数强制转换为TMyType指针?

如下所示:

type
  PMyType = ^TMyType;

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

您必须使用新类型显式地强制转换它:

  type PMyType = ^TMyType;

  ptr := PMyType(bar);


啊,我不确定是否需要一种新的类型。我认为这就像C语言中的“类型转换”是纯粹的optional@Earlz类型转换在C中不是可选的。在C中为指针变量指定一个整数需要进行转换。Pascal对象是强类型语言,因此在不同类型之间强制转换。@TridenT C也是强类型的。事实上,C和Delphi的相似之处在于,任何指针都与
void*
/
指针
的赋值兼容。请注意,在Win64上,这将失败,因为那里的指针类型是64位,整数仍然是32位
  ptr := pointer(bar);