您的位置: 首页-> 电脑文摘-> 程序设计-> Delphi/Pascal-> 正文

指针运算
作者佚名 来源InterNet 加入时间:2004-11-22

uses WinCRT;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyArray: array[0..30] of char;
  b: ^char;
  i: integer;

begin
  StrCopy(MyArray, 'Lloyd is the greatest!'); {get something to point to}
  b := @MyArray; { assign the pointer to the memory location }
  for i := StrLen(MyArray) downto 0 do
  begin
    write(b^);   { write out the char at the current pointer location. }
    inc(b);      { point to the next byte in memory }
  end;
end;

The following code demonstrates that the Inc() and Dec() functions
will increment or decrement accordingly by size of the type the pointer

points to:

var
  P1, P2 : ^LongInt;
  L : LongInt;
begin
  P1 := @L; { assign both pointers to the same place }
  P2 := @L;
  Inc(P2);  { Increment one }

{ Here we get the difference between the offset values of the
two pointers.  Since we originally pointed to the same place in
memory, the result will tell us how much of a change occured
when we called Inc(). }

  L := Ofs(P2^) - Ofs(P1^); { L = 4; i.e. sizeof(longInt) }
end;



[文章录入员:fightter]

相关文章 相关软件:
::PCBOOKCN'ADS::


::Delphi/Pascal::
C/C++/VC
C++Builder
Basic/VB类
Delphi/Pascal
Java编程
FORTRAN
其它
::阅读排行::
·Delphi与Excel的亲密接...
·Delphi实现串口通信的常用的...
·DELPHI下的多线程程序设计(...
·Delphi中初始化.Ini文件...
·Delphi中用于读写(I/O)...
·谈谈Delphi中的类和对象
·Delphi编译错误中文手册
·Delphi中关于TApplic...
·OPENGL图形程序设计
·SQL的基本操作