Pascal 指针指向指针

  • 指针指向指针

    指向指针的指针是多种间接形式或一系列指针。通常,指针包含变量的地址。当我们定义指向指针的指针时,第一个指针包含第二个指针的地址,该地址指向包含实际值的位置,如下所示。
    pointer
    指向指针的变量必须这样声明。例如,
    
    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 = 50960
    Address at pptr = 50976