在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi 的指针分为 "类型指针" 和 "无类型指针" 两类.
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //Integer 与 PInteger procedure TForm1.Button1Click(Sender: TObject); var int: Integer; pint: PInteger; {定义类型指针, Integer 类型的指针} begin int := 100; pint := @int; {现在 pint 就是 int 的指针} pint^ := pint^ + 1; {现在 pint^ 和 int 是一回事, 测试一下:} ShowMessage(IntToStr(int)); {101} ShowMessage(IntToStr(pint^)); {101} end; //直接定义类型指针 procedure TForm1.Button2Click(Sender: TObject); var int: Integer; PMyInt: ^Integer; begin int := 100; PMyInt := Addr(int); {这句和: PMyInt := @int; 相同} PMyInt^ := PMyInt^ + 1; ShowMessage(IntToStr(int)); {101} ShowMessage(IntToStr(PMyInt^)); {101} end; //先自定义指针类型 procedure TForm1.Button3Click(Sender: TObject); type PInt = ^Integer; var int: Integer; PMyInt: PInt; begin int := 100; PMyInt := @int; PMyInt^ := PMyInt^ + 1; ShowMessage(IntToStr(int)); {101} ShowMessage(IntToStr(PMyInt^)); {101} end; //指针的指针 procedure TForm1.Button4Click(Sender: TObject); var int: Integer; pint: PInteger; ppint: ^PInteger; begin int := 100; pint := @int; ppint := @pint; ppint^^ := ppint^^ + 1; ShowMessage(IntToStr(int)); {101} ShowMessage(IntToStr(pint^)); {101} ShowMessage(IntToStr(ppint^^)); {101} end; end. 知道以上这些就可以操作了, 就可以看懂别人的代码了; 不过要想彻底明白指针到底是怎么回事, 需要从内存谈起. 在来学习下,通过创建线程CreateThread,通过指针传递参数到线程里面去; function myFun(ID:PInteger):boolean;stdcall; begin form1.Memo1.Lines.Add('ID='+intTostr(ID^)); end; procedure TForm1.Button3Click(Sender: TObject); var ID : Pinteger; begin new(ID); //分配内存空间 ID^ := 2; //怎么把ID=2传到myFun里面去 CreateThread(nil, 0, @myFun, ID, 0, TID); Dispose(ID);//释放内存空间 end;
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论