指向指针( Pointer to pointer)
优质
小牛编辑
127浏览
2023-12-01
指向指针的指针是多个间接或指针链的形式。 通常,指针包含变量的地址。 当我们定义指向指针的指针时,第一个指针包含第二个指针的地址,它指向包含实际值的位置,如下所示。
必须声明一个指向指针的指针的变量。 例如,
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