Search code examples
pointerspascaldynamic-variables

Pointer to an array of pointers in Pascal


I don't know how to access the content of an array of pointers by a pointer. Here's an example:

Type
    PInteger = ^Integer;
    IntegerArrayP = array of PInteger;
    PIntegerArrayP = ^IntegerArray;

var
    variable: Integer;
    parrp: PIntegerArrayP;
    arrp: IntegerArrayP;
begin
    SetLength(arrp, 5);
    parrp := @arrp;
    For variable := Low(arrp) to High(arrp) do
    begin
        arrp[variable] := New(PInteger);
        (parrp^)[variable]^ := variable;
        WriteLn('parrp: ', arrp[variable]^);
    end;
end.

In my opinion it should be done like this (ptabp^)[variable]^ := variable; But I guess I'm wrong.


Solution

  • You are right. Parens might be omitted.
    What pascal compiler do you use? Proper usage of New routine:

     New(arrp[variable]) ;
     parrp^[variable]^ := variable;
    

    P.S. Do you really need these pointer types here?

    P.P.S. Now I see an error: PIntegerArrayP = ^IntegerArrayP;