指针问题

wslmsx 2009-10-09 11:52:29
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
StringArray=array of string;
PStringArray=^StringArray;
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function seqsearch(astrs:pstringarray;acount:integer;const aname:string):integer;
public
{ Public declarations }
end;


var
Form1: TForm1;
a:array[0..4] of string=
('12345',
'23456',
'34567',
'45678',
'56789');
p:PStringArray;
implementation

{$R *.dfm}
function tform1.seqsearch(astrs:pstringarray;acount:integer;const aname:string):integer;
var
i:integer;
begin
for i:=0 to pred(acount) do
if comparetext(astrs^[i],aname)=0 then
begin
result:=i;
exit;
end;
result:=-1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
p:=@a;
edit1.Text:=inttostr(seqsearch(p,5,'34567'));
end;

end.
运行时点击按钮出错,停在了if comparetext(astrs^[i],aname)=0这句上。请问为什么,怎样解决?谢谢!
...全文
73 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wslmsx 2009-10-09
  • 打赏
  • 举报
回复
如果就要用指针的形式呢,程序里应该怎么改?
skertone 2009-10-09
  • 打赏
  • 举报
回复
用开放数组好了


改成
function tform1.seqsearch(astrs:array of string;acount:integer;const aname:string):integer;

if comparetext(astrs[i],aname)=0 then

edit1.Text:=inttostr(seqsearch(a,5,'34567'));
jadeluo 2009-10-09
  • 打赏
  • 举报
回复
动态数组 array of string 和 array[0..4] of string 在内存中的结构是不同的, 改成这样:

var
a:array of string;
p:PStringArray;

begin
SetLength(a, 5);
a[0] := '12345';
a[1] := '23456';
a[2] := '34567';
a[3] := '45678';
a[4] := '56789';
p:=@a;
ShowMessage(IntToStr(seqsearch(p, 5, '34567')));
end;
火龙岛主 2009-10-09
  • 打赏
  • 举报
回复
测试是访问地址错误:if comparetext(astrs^[i],aname)=0 then
还没这么用过,一般情况下,我们使用TStringList来处理类似问题!
haitao 2009-10-09
  • 打赏
  • 举报
回复
编译出错(错误信息是什么?)
还是
运行出错?

p:=@a;
改为
p:=@(a[0]);
试一试

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧