[fpc-pascal] Pass array of record as parameter

Howard Page-Clark hdpc at talktalk.net
Thu Feb 9 19:08:58 CET 2012


On 09/2/12 5:17, Zaher Dirkey wrote:
> Hi,
>
> TInfo=record
>    s: string;
>    i: integer;
> end;
>
> function func1(a: array of TInfo);
>
> how can i pass the parameter to that function?

You have to declare the parameter type independently of the function 
declaration. You can also do it without declaring an additional pointer 
type (which is also one way to do it), since dynamic arrays already have 
an implicit pointer. Something like this:

type
   TInfo=record
     s: string;
     i: integer;
   end;

   TInfoArr = array of tInfo;

procedure proc1(a: TInfoArr);

implementation

procedure TForm1.Button1Click(Sender: TObject);
var rec: TInfo;
     infoArr: TInfoArr = nil;
begin
   SetLength(infoArr, 2);
   rec.i := 1; rec.s := 'test1';
   infoArr[0] := rec;
   rec.i := 2; rec.s := 'test2';
   infoArr[1] := rec;
   proc1(infoArr);
end;

procedure proc1(a: TInfoArr);
var s: string = '';
     i: integer;
begin
   for i := Low(a) to High(a) do
    s := s + Format('TInfo%d: s=%s, i=%d ',[i, a[i].s, a[i].i]);
   Showmessage(s);
end;



More information about the fpc-pascal mailing list