请教,数组指针 的初始化和使用?
定义了3个数组
KeyWords:array[0..3]of string=( 'main ', 'if ', 'int ', 'for ');
operator:array[0..3]of string=( '+ ', '- ', '* ', '/ ');
separator:array[0..1]of string=( '; ', ', ');
问题1.想定义并初始化一个数组指针alltype,用于存放3个数组的首地址,怎么操作?
问题2.怎么利用数组指针alltype,for语句对3个数组遍历
var i:integer
begin
for i:=0 to 2 do
begin
search(alltype[i]);//前面定义好的过程
i:=i+1;
end;
end;
指针数组、数组指针没学好,大家帮帮忙,呵呵.
[解决办法]
问题1.想定义并初始化一个数组指针alltype,用于存放3个数组的首地址,怎么操作?
----------------------
type
TAllType = array[0..2] of pointer;
TKeyWords = array[0..3]of string;
TSeparator = array[0..1]of string;
const
KeyWords : TKeyWords = ( 'main ', 'if ', 'int ', 'for ');
operator : TKeyWords =( '+ ', '- ', '* ', '/ ');
separator: TSeparator =( '; ', ', ');
var
alltype : TAllType;
procedure TForm1.Button1Click(Sender: TObject);
var
P : ^TKeyWords;
begin
allType[0] := @KeyWords[Low(KeyWords)];
allType[1] := @operator[Low(operator)];
allType[2] := @separator[Low(separator)];
P := allType[0]; //下面两句是演示通过保存的数组首地址, 访问数组元素.
showmessage(P^[1])
end;
问题2 :
既然allType是指针数组, 那么恐怕Search过程只有一个参数还不够. 估计还需要传递数组类型 及数组大小吧, 要不, 怎么用.
没时间了, 你等别人回答问题二吧.
[解决办法]
var
KeyWords:array[0..3]of string=( 'main ', 'if ', 'int ', 'for ');
operator:array[0..3]of string=( '+ ', '- ', '* ', '/ ');
separator:array[0..1]of string=( '; ', ', ');
procedure TForm1.Button1Click(Sender: TObject);
var
Alltype : array [0..2] of Pointer;
p : Pointer;
i : Integer;
begin
//等价于Alltype[0] := @KeyWords
Alltype[0] := @KeyWords[0];
Alltype[1] := @operator[0];
Alltype[2] := @separator[0];
//遍历KeyWords
p := Alltype[0];
for i:=Low(KeyWords) to High(KeyWords) do
begin
ShowMessage(PString(p)^);
Inc(PString(p));
end;
//遍历operator
p := Alltype[1];
for i:=Low(operator) to High(operator) do
begin
ShowMessage(PString(p)^);
Inc(PString(p));
end;
//遍历separator
p := Alltype[2];
for i:=Low(separator) to High(separator) do
begin
ShowMessage(PString(p)^);
Inc(PString(p));
end;
end;