Ada*_*oan 25 delphi pascal pointers dereference
在Delphi/Free Pascal中:是一个运算符还是它只是表示一个指针类型?
program Project1;
{$APPTYPE CONSOLE}
var
P: ^Integer;
begin
New(P);
P^ := 20;
writeln(P^); // How do I read this statement aloud? P is a pointer?
Dispose(P);
readln;
end
Run Code Online (Sandbox Code Playgroud)
Cod*_*aos 40
当^用作类型的一部分时(通常在类型或变量声明中),它表示"指向".
例:
type
PInteger = ^Integer;
Run Code Online (Sandbox Code Playgroud)
当^用作一元后缀运算符时,它表示"取消引用指针".所以在这种情况下,它意味着"打印P指向什么"或"打印目标P".
例:
var
i: integer;
a: integer;
Pi: PInteger;
begin
i:= 100;
Pi:= @i; <<--- Fill pointer to i with the address of i
a:= Pi^; <<--- Complicated way of writing (a:= i)
<<--- Read: Let A be what the pointer_to_i points to
Pi^:= 200;<<--- Complicated way of writing (i:= 200)
writeln('i = '+IntToStr(i)+' and a = '+IntToStr(a));
Run Code Online (Sandbox Code Playgroud)