指向Delphi中指针的指针

时间:2013-08-04 15:45:29

标签: c++ delphi pointers

如何在Delphi中编写此代码(C ++:指针指针)?

   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;

2 个答案:

答案 0 :(得分:8)

你可以这样做

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^^); 

答案 1 :(得分:-2)

下面的代码工作正常:

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.