level 1
z_g_j_
楼主
//第一个例子代码
typeTMyArray=array of integer;
procedure f(arr:TMyArray);
begin
arr[5]:=103;
end;
procedure TForm1.Button1Click(Sender:TObject);
var a:TMyArray;
begin
setlength(a,10);
showmessage(inttostr(a[5]));//显示0
f(a);
showmessage(inttostr(a[5]));//显示103
end;
//从第一个例子可以看出动态数组作为例程参数时是地址传递
//第二个例子代码
type TMyArray=array of integer;
procedure f(arr:TMyArray);
begin
setlength(arr,20);
showmessage(inttostr(length(arr)));
end;
procedure TForm1.Button1Click(Sender:TObject);
var a:TMyArray;
begin
setlength(a,10);
showmessage(inttostr(length(a)));//显示10
f(a); //显示20
showmessage(inttostr(length(a)));//显示10
end;
//从第二个例子似乎又说明动态数组作为例程参数时不是地址传递,不然最后一个数组长度应该是20
//第三个例子代码
type TMyArray=array of integer;
procedure f(var arr:TMyArray);
begin
setlength(arr,20);
showmessage(inttostr(length(arr)));
end;
procedure TForm1.Button1Click(Sender:TObject);
var a:TMyArray;
begin
setlength(a,10);
showmessage(inttostr(length(a)));//显示10
f(a); //显示20
showmessage(inttostr(length(a)));//显示20
end;
//第三个例子代码将例程参数前加了var,结果就跟第二个例子不一样了,我的问题是如果动态数组作为例程参数时是以地址传递的,为什么还要加var。
2015年12月23日 08点12分
1
typeTMyArray=array of integer;
procedure f(arr:TMyArray);
begin
arr[5]:=103;
end;
procedure TForm1.Button1Click(Sender:TObject);
var a:TMyArray;
begin
setlength(a,10);
showmessage(inttostr(a[5]));//显示0
f(a);
showmessage(inttostr(a[5]));//显示103
end;
//从第一个例子可以看出动态数组作为例程参数时是地址传递
//第二个例子代码
type TMyArray=array of integer;
procedure f(arr:TMyArray);
begin
setlength(arr,20);
showmessage(inttostr(length(arr)));
end;
procedure TForm1.Button1Click(Sender:TObject);
var a:TMyArray;
begin
setlength(a,10);
showmessage(inttostr(length(a)));//显示10
f(a); //显示20
showmessage(inttostr(length(a)));//显示10
end;
//从第二个例子似乎又说明动态数组作为例程参数时不是地址传递,不然最后一个数组长度应该是20
//第三个例子代码
type TMyArray=array of integer;
procedure f(var arr:TMyArray);
begin
setlength(arr,20);
showmessage(inttostr(length(arr)));
end;
procedure TForm1.Button1Click(Sender:TObject);
var a:TMyArray;
begin
setlength(a,10);
showmessage(inttostr(length(a)));//显示10
f(a); //显示20
showmessage(inttostr(length(a)));//显示20
end;
//第三个例子代码将例程参数前加了var,结果就跟第二个例子不一样了,我的问题是如果动态数组作为例程参数时是以地址传递的,为什么还要加var。