unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TRec = record {定义结构 TRec}
name: string;
age: Word;
end;
TpRec = ^TRec; {定义 TRec 结构的指针类型 TPRec}
var
Rec: TRec; {声明结构变量}
pRec1,pRec2: TpRec; {声明 TPRec 指针变量}
PRec3: ^TRec; {声明 TRec 结构的指针变量, 现在 PRec1 与 PRec3 是不同类型的变量}
P: Pointer; {声明无类型指针}
procedure TForm1.Button1Click(Sender: TObject);
begin
Rec.name := '张三';
Rec.age := 18;
PRec1 := @Rec; {把 Rec 的地址告诉 PRec1}
ShowMessage(PRec1^.name); {张三}
{Delphi 允许这样简单使用结构指针}
ShowMessage(PRec1.name); {张三}
end;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TRec = record {定义结构 TRec}
name: string;
age: Word;
end;
TpRec = ^TRec; {定义 TRec 结构的指针类型 TPRec}
var
Rec: TRec; {声明结构变量}
pRec1,pRec2: TpRec; {声明 TPRec 指针变量}
PRec3: ^TRec; {声明 TRec 结构的指针变量, 现在 PRec1 与 PRec3 是不同类型的变量}
P: Pointer; {声明无类型指针}
procedure TForm1.Button1Click(Sender: TObject);
begin
Rec.name := '张三';
Rec.age := 18;
PRec1 := @Rec; {把 Rec 的地址告诉 PRec1}
ShowMessage(PRec1^.name); {张三}
{Delphi 允许这样简单使用结构指针}
ShowMessage(PRec1.name); {张三}
end;
//如果是单独使用指针, 必须先给内存
procedure TForm1.Button2Click(Sender: TObject);
begin
GetMem(PRec2, SizeOf(TRec));
PRec2.name := '王五';
PRec2.age := 9;
ShowMessage(PRec2.name); {王五}
{手工给的内存, 必须手工释放}
FreeMem(PRec2);
end;
end.