var
Pi:Pointer;
A,B:Integer;
begin
A:=1;B:=2;
Pi:=@B;
A:=Integer(Pi^);
ShowMessage(IntToStr(A));
end;
参看帮助中关于Pointer这一节,说But a Pointer variable cannot be dereferenced; placing the ^ symbol after a Pointer variable causes a compilation error.即无类型指针是不能解除引用的,但为什么上述代码能正确编译并得到正确答案呢?
Pi:Pointer;
A,B:Integer;
begin
A:=1;B:=2;
Pi:=@B;
A:=Integer(Pi^);
ShowMessage(IntToStr(A));
end;
参看帮助中关于Pointer这一节,说But a Pointer variable cannot be dereferenced; placing the ^ symbol after a Pointer variable causes a compilation error.即无类型指针是不能解除引用的,但为什么上述代码能正确编译并得到正确答案呢?
应该是当成PInteger来处理了吧
Pi:Pointer;
A,B:Integer;
begin
A:=1;
B:=$12345678;
Pi:=@B;
A:=Integer(Pi^);
ShowMessage(IntToHex(A,8));
A:=Byte(Pi^);
ShowMessage(IntToHex(A,8));
end;
p: Pointer;
pInt: PInteger;
a, b: Integer;
begin
a := 2;
pInt := @a;
p := @a;
b := pInt^; // pInt为强类型指针,解除引用后可以直接赋值给对应的类型
b := p^; // p为无类型指针,解除引用后不能直接复制给强类型值,需进行转换,如: b := PInteger(p)^; 或 b := Integer(p^);
end;