51代码网ORACLEMYSQLSQL SERVER其它数据库java/jspasp/asp.netC/C++/VC++APP应用其它语言服务器应用
您现在的位置: 51代码网 >> 其它 >> 文章正文

delphi数组循环报错

更新时间:2012-4-1:  来源:51代码网

procedure TForm1.Button1Click(Sender: TObject);
var
  d:array of string;
  i:integer;
begin
  a:=Form1.Label1.Caption;
  Form1.Label1.Caption:='';
  b:=length(a);
  c:= b div 5 ;
  SetLength(d,c);
  for i:=0 to c do
  begin
  arr[i]:=Copy(a,5*(i)+1,5);
  Form1.Label1.Caption:=Form1.Label1.Caption+arr[i]+#13#10;  
  end;
end.
程序代码如上 用以实现label1的长度5自动换行功能
编译可以通过 但是运行的时候 弹出提示
Project Project.exe raised exception class EAccessViolation with message `Access violation at address 00403E92 in module `Project1.exe Read of address 0000000A. Process stopped. Use Step or Run to continue.
求解报错原因及解决方法


SetLength(d,c);
d长度不够,若刚好整除,则没问题,否则要+1
if (b mod 5)>0 then
SetLength(d,c+1)
else
SetLength(d,c);

后面循环最好不要用c,用这个:
for i:=low(d) to high(d) do

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 赞助商链接
    推荐文章
  • 此栏目下没有推荐文章
  • {
    设为首页 | 加入收藏 | 友情链接 | 网站地图 | 联系站长 |