Tbytes 就是动态数组 array of byte。以下代码在 Delphi 10 中编译通过
1. 静态数组
procedure TForm1.Button1Click(Sender: TObject); var I:Byte; s:string; Buf:array[0..3] of Byte; begin //---------------------------传址是Buf[0] for I := 0 to 3 do Begin Buf[I]:=1; End; Test(Buf[0]);//同Test(Buf); s:=''; for I := 0 to 3 do Begin s:=s+inttostr(Buf[I]); End; showmessage(s); //输出结果是:2211 //---------------------------更改一下传址 for I := 0 to 3 do Begin Buf[I]:=1; End; Test(Buf[1]);//改为 Buf[1] s:=''; for I := 0 to 3 do Begin s:=s+inttostr(Buf[I]); End; showmessage(s); //输出结果是:1221 end;
2. TBytes 和动态数组
procedure TForm1.Button2Click(Sender: TObject); var I:Byte; s:string; Buf:TBytes;//同 Buf:array of Byte; begin Setlength(Buf,4); //---------------------------传址是Buf[0] for I := 0 to 3 do Begin Buf[I]:=1; End; Test(Buf);//同Test(Buf); s:=''; for I := 0 to 3 do Begin s:=s+inttostr(Buf[I]); End; showmessage(s); //输出结果是:2211 //---------------------------更改一下传址 for I := 0 to 3 do Begin Buf[I]:=1; End; Test(Buf[1]);//改为 Buf[1] s:=''; for I := 0 to 3 do Begin s:=s+inttostr(Buf[I]); End; showmessage(s); //输出结果是:1221 end;
procedure TForm1.Test(var Buf:array of Byte); begin Buf[0]:=2; Buf[1]:=2; end;