Pascal - 指標的指標



指標的指標是多重間接定址或指標鏈的一種形式。 通常,指標包含變數的地址。 當我們定義指標的指標時,第一個指標包含第二個指標的地址,該指標指向包含實際值得位置,如下所示。

Pointer to Pointer in 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
廣告