mohammadkad
mohammadkad

Reputation: 105

pointer to a pointer in Delphi

How Can I Write This Code ( C++ : Pointer to Pointer ) In Delphi ?

   int  var;
   int  *ptr;
   int  **pptr;

   var = 3000;
   ptr = &var;
   pptr = &ptr;

   cout << "Value of var :" << var << endl;
   cout << "Value available at *ptr :" << *ptr << endl;
   cout << "Value available at **pptr :" << **pptr << endl;

Upvotes: 1

Views: 1959

Answers (2)

Alexandr
Alexandr

Reputation: 338

Code below works fine:

program Project1;

{$APPTYPE CONSOLE}

type
  PIntPtr = ^Integer;
  PInt2Ptr = ^TIntPtr;

var
  pi1: PIntPtr;
  pi2: PInt2Ptr;

begin
  New(pi1);
  try
    New(pi2);
    try
      pi1^ := 1000;
      pi2^ := i1;
      WriteLn(pi2^^);
      Readln;
    finally
      Dispose(pi2);
    end;
  finally
    Dispose(pi1);
  end;
end.

Upvotes: -2

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108929

You can do like this

var
  i: Integer;
  pi: PInteger;       // or ^Integer
  ppi: ^PInteger;     // or PPInteger, if you first define `type PPInteger = ^PInteger`
begin

  i := 3000;
  pi := @i;
  ppi := @pi;

  Writeln('Value of i: ', i);
  Writeln('Value of i: ', pi^);
  Writeln('Value of i: ', ppi^^); 

Upvotes: 10

Related Questions