type
PTCsRc = ^TCsRc;
TCsRc = record
ID:Word;
Count:Integer;
end;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FP:PTCsRc;
FB:PAnsiChar;
FPDestroy:PAnsiChar;
public
{ Public declarations }
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
GetMem(FB,SizeOf(TCsRc) * 3);
FPDestroy := FB;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeMem(FPDestroy);
FPDestroy := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I:Integer;
nCs:TCsRc;
nP:PAnsiChar;
begin
nP := FB;
FP := @FB[0];//也可以强制转换:PTCsRc(FB)
for I := 0 to 3 - 1 Do begin
FillChar(nCs,SizeOf(TCsRc),0);
With nCs Do begin
ID := I;
Count := SizeOf(TCsRc) + I;
end;
Move(nCs,FB^,SizeOf(TCsRc));
Inc(FB ,SizeOf(TCsRc) ); //这里需要加上结构大小
Memo1.Lines.Add(Format('%d',[nCs.ID]));
Memo1.Lines.Add(Format('%d',[nCs.Count]));
Memo1.Lines.Add('========');
end;
for I := 0 to 3 - 1 Do begin
FillChar(nCs,SizeOf(TCsRc),0);
Move(nP^,nCs,SizeOf(TCsRc));
Memo1.Lines.Add(Format('%d',[nCs.ID]));
Memo1.Lines.Add(Format('%d',[nCs.Count]));
Memo1.Lines.Add('***********');
Inc(nP,SizeOf(TCsRc)); //这里需要加上结构大小
end;
for I := 0 to 3 - 1 Do begin
FillChar(nCs,SizeOf(TCsRc),0);
Move(FP^,nCs,SizeOf(TCsRc));
Memo1.Lines.Add(Format('%d',[nCs.ID]));
Memo1.Lines.Add(Format('%d',[nCs.Count]));
Memo1.Lines.Add('-----------');
Inc(FP); //结构指针这里直接增加
end;
end;