结构化指针和单字节指针的区别
Posted gamedelphi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了结构化指针和单字节指针的区别相关的知识,希望对你有一定的参考价值。
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;
以上是关于结构化指针和单字节指针的区别的主要内容,如果未能解决你的问题,请参考以下文章