转自:万一老师的 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}
-
-
type
-
TRec = record
-
name: string[12];
- age: Word;
-
end;
-
TPRec = ^TRec;
-
-
var
-
Rec: TRec;
-
PRec1,PRec2: TPRec;
-
PRec3: ^TRec;
-
P: Pointer;
-
-
-
-
procedure TForm1.Button1Click(Sender: TObject);
-
begin
-
Rec.name := '张三';
-
Rec.age := 18;
-
-
PRec1 := @Rec;
-
-
-
ShowMessage(PRec1^.name);
-
-
-
ShowMessage(PRec1.name);
-
-
-
PRec1.name := '李四';
-
-
-
ShowMessage(Rec.name);
-
-
end;
-
-
-
-
procedure TForm1.Button2Click(Sender: TObject);
-
begin
- GetMem(PRec2, SizeOf(TRec));
-
PRec2.name := '王五';
-
PRec2.age := 9;
-
-
ShowMessage(PRec2.name);
-
-
- FreeMem(PRec2);
-
end;
-
-
-
-
procedure TForm1.Button3Click(Sender: TObject);
-
begin
-
Rec.name := '孙六';
-
Rec.age := 16;
-
-
- PRec3 := @Rec;
-
ShowMessage(PRec3.name);
-
-
- PRec1 := TPRec(PRec3);
-
ShowMessage(PRec1.name);
-
-
-
-
Rec.name := '赵七';
-
Rec.age := 24;
- PRec1 := @Rec;
-
ShowMessage(PRec1.name);
-
-
- TPRec(PRec3) := PRec1;
-
ShowMessage(PRec3.name);
-
end;
-
-
-
-
procedure TForm1.Button4Click(Sender: TObject);
-
begin
-
Rec.name := '杜八';
-
Rec.age := 36;
-
- P := @Rec;
-
-
-
TPRec(P).name := '侯九';
-
-
-
ShowMessage(TPRec(P).name);
-
end;
-
-
end.
|
请发表评论