Pascal - 指针到指针


指向指针的指针是多重间接寻址或指针链的一种形式。通常,指针包含变量的地址。当我们定义一个指向指针的指针时,第一个指针包含第二个指针的地址,第二个指针指向包含实际值的位置,如下所示。

Pascal 中的指针到指针

作为指向指针的指针的变量必须如此声明。例如,

type
   iptr = ^integer;
   pointerptr = ^ iptr;

以下示例将说明该概念并显示地址 -

program exPointertoPointers;
type
   iptr = ^integer;
   pointerptr = ^ iptr;

var
   num: integer;
   ptr: iptr;
   pptr: pointerptr;
   x, y : ^word;

begin
   num := 3000;
   
   (* take the address of var *)
   ptr := @num;
   
   (* take the address of ptr using address of operator @ *)
   pptr := @ptr;
   
   (* let us see the value and the adresses *)
   x:= addr(ptr);
   y := addr(pptr);
   
   writeln('Value of num = ', num );
   writeln('Value available at ptr^ = ', ptr^ );
   writeln('Value available at pptr^^ = ', pptr^^);
   writeln('Address at ptr = ', x^); 
   writeln('Address at pptr = ', y^);
end.

当上面的代码被编译并执行时,它会产生以下结果 -

Value of num = 3000
Value available at ptr^ = 3000
Value available at pptr^^ = 3000
Address at ptr = 45664
Address at pptr = 45680
pascal_pointers.htm