Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-02

Pass array of record as parameter

20 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Zaher Dirkey

Hi,

TInfo=record
  s: string;
  i: integer;
end;
function func1(a: array of TInfo);

how can i pass the parameter to that function?

for example, i cant do that?

func1([('test1', 1), ('test2', 2)]);
I am using last reversion of Lazarus, FPC 2.6 Thanks in advance Zaher Dirkey

Reply by email

Everton Vieira
to Zaher Dirkey

That`s a question of mine too.

Zaher Dirkey wrote 25 lines
Hi, TInfo=record s: string; i: integer; end; function func1(a: array of TInfo); how can i pass the parameter to that function? for example, i cant do that? func1([('test1', 1), ('test2', 2)]); -- I am using last reversion of Lazarus, FPC 2.6 Thanks in advance Zaher Dirkey _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Thomas Young
to Zaher Dirkey: TInfo=record s: string; i: integer; end; function func1(a: array of TInfo); how can i…

Define a pointer type for TInfo then pass the pointer to the function. Dereference the pointer within the function to access the array.

Thomas Young
330-256-7064
www.tygraphics.net
Sent from my iPhone

Zaher Dirkey wrote 25 lines
Hi, TInfo=record s: string; i: integer; end; function func1(a: array of TInfo); how can i pass the parameter to that function? for example, i cant do that? func1([('test1', 1), ('test2', 2)]); -- I am using last reversion of Lazarus, FPC 2.6 Thanks in advance Zaher Dirkey _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Everton Vieira
to Thomas Young

Could you give an example? If not bother much.

Thomas Young wrote 37 lines
Define a pointer type for TInfo then pass the pointer to the function. Dereference the pointer within the function to access the array. Thomas Young 330-256-7064 www.tygraphics.net Sent from my iPhone On Feb 9, 2012, at 12:17 PM, 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? > > for example, i cant do that? > > func1([('test1', 1), ('test2', 2)]); > > -- > I am using last reversion of Lazarus, FPC 2.6 > > Thanks in advance > > Zaher Dirkey > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Thomas Young
to Everton Vieira: That`s a question of mine too.

This is how I would do that. There may be a better way.

Show quoted text 5 lines
TInfo=record s: string; i: integer; end;
ATInfo = array[1..100] of TInfo;
ATInfoPtr = ^ATInfo;
Var
A:ATInfoPtr;
function func1(a:ATInfoPtr);

Thomas Young
330-256-7064
www.tygraphics.net
Sent from my iPhone

Everton Vieira wrote 6 lines
> TInfo=record > s: string; > i: integer; > end; > > function func1(a: array of TInfo);

Reply by email

Everton Vieira
to Thomas Young

Thanks.

Thomas Young wrote 31 lines
This is how I would do that. There may be a better way. > TInfo=record > s: string; > i: integer; > end; > ATInfo = array[1..100] of TInfo; ATInfoPtr = ^ATInfo; Var A:ATInfoPtr; > function func1(a:ATInfoPtr); Thomas Young 330-256-7064 www.tygraphics.net Sent from my iPhone On Feb 9, 2012, at 12:24 PM, Everton Vieira wrote: >> TInfo=record >> s: string; >> i: integer; >> end; >> >> function func1(a: array of TInfo); _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Thomas Young
to Thomas Young: This is how I would do that. There may be a better way. ATInfo = array[1..100] of TInfo;…
New(A);
> function func1(a:ATInfoPtr);
      var
         Name:string;
         N:integer;
      begin
         N:= 1;
         Name:= a^[n].s;
      end;

Thomas Young
330-256-7064
www.tygraphics.net
Sent from my iPhone

Thomas Young wrote 31 lines
This is how I would do that. There may be a better way. > TInfo=record > s: string; > i: integer; > end; > ATInfo = array[1..100] of TInfo; ATInfoPtr = ^ATInfo; Var A:ATInfoPtr; > function func1(a:ATInfoPtr); Thomas Young 330-256-7064 www.tygraphics.net Sent from my iPhone On Feb 9, 2012, at 12:24 PM, Everton Vieira wrote: >> TInfo=record >> s: string; >> i: integer; >> end; >> >> function func1(a: array of TInfo); _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Zaher Dirkey
to Thomas Young: This is how I would do that. There may be a better way. ATInfo = array[1..100] of TInfo;…
Thomas Young wrote 40 lines
This is how I would do that. There may be a better way. TInfo=record s: string; i: integer; end; ATInfo = array[1..100] of TInfo; ATInfoPtr = ^ATInfo; Var A:ATInfoPtr; function func1(a:ATInfoPtr); Thomas Young 330-256-7064 www.tygraphics.net Sent from my iPhone On Feb 9, 2012, at 12:24 PM, Everton Vieira wrote: TInfo=record s: string; i: integer; end; function func1(a: array of TInfo);

Thanks, but that is not my expected answer, my ask about "dynamic array of record" and without define a variable to pass it by params, for example i can pass the params for array of string like this

func2(['t1', 't1']); this work, but i want to extend the params info?

Best Regards
Zaher Dirkey

Reply by email

Everton Vieira
to Everton Vieira: Thanks.

But they couldn't pass like this?:

Show quoted text 13 lines
> This is how I would do that. There may be a better way. > >> TInfo=record >> s: string; >> i: integer; >> end; >> > ATInfo = array[1..100] of TInfo; > > Var > A :ATInfo; > >> function func1(A);

Simply passing the array var if he had declared.
I guess the question of our friend was about to how to pass an array with record without the need to declare.

Everton Vieira wrote 36 lines
Thanks. Em 09/02/2012, às 15:53, Thomas Young escreveu: > This is how I would do that. There may be a better way. > >> TInfo=record >> s: string; >> i: integer; >> end; >> > ATInfo = array[1..100] of TInfo; > ATInfoPtr = ^ATInfo; > > Var > A:ATInfoPtr; > >> function func1(a:ATInfoPtr); > > Thomas Young > 330-256-7064 > www.tygraphics.net > Sent from my iPhone > > On Feb 9, 2012, at 12:24 PM, Everton Vieira wrote: > >>> TInfo=record >>> s: string; >>> i: integer; >>> end; >>> >>> function func1(a: array of TInfo); > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Howard Page-Clark
to Zaher Dirkey: TInfo=record s: string; i: integer; end; function func1(a: array of TInfo); how can i…
Zaher Dirkey wrote 10 lines
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;

Reply by email

Jonas Maebe
to Zaher Dirkey: Thanks, but that is not my expected answer, my ask about "dynamic array of record" and…
Zaher Dirkey wrote 5 lines
Thanks, but that is not my expected answer, my ask about "dynamic array of record" and without define a variable to pass it by params, for example i can pass the params for array of string like this func2(['t1', 't1']); this work, but i want to extend the params info?

Declaring record constants inline in the code is not supported by FPC (regardless of whether it's for an array parameter, an assignment or something else). There are no plans to add support for it either.

Jonas

Reply by email

Ludo Brands
to Zaher Dirkey: Thanks, but that is not my expected answer, my ask about "dynamic array of record" and…

Thanks, but that is not my expected answer, my ask about "dynamic array of record" and without define a variable to pass it by params, for example i can pass the params for array of string like this

func2(['t1', 't1']); this work, but i want to extend the params info?

You can use a small helper function like this:

type
  TInfo=record
    s: string;
    i: integer;
  end;
  function makeinfo(s:string;i:integer):TInfo;
  begin
    result.s:=s;
    result.i:=i;
  end;

...

  func1([makeinfo('test1', 1), makeinfo('test2', 2)]);

Ludo

Reply by email

Zaher Dirkey
to Ludo Brands: Thanks, but that is not my expected answer, my ask about "dynamic array of record" and…
Ludo Brands wrote 30 lines
** Thanks, but that is not my expected answer, my ask about "dynamic array of record" and without define a variable to pass it by params, for example i can pass the params for array of string like this func2(['t1', 't1']); this work, but i want to extend the params info? You can use a small helper function like this: type TInfo=record s: string; i: integer; end; function makeinfo(s:string;i:integer):TInfo; begin result.s:=s; result.i:=i; end; ... func1([makeinfo('test1', 1), makeinfo('test2', 2)]);

Yes, it is good idea, i will try it :D

Best Regards
Zaher Dirkey

Reply by email

Everton Vieira
to Ludo Brands: Thanks, but that is not my expected answer, my ask about "dynamic array of record" and…

That`s a nice idea.

Ludo Brands wrote 28 lines
Thanks, but that is not my expected answer, my ask about "dynamic array of record" and without define a variable to pass it by params, for example i can pass the params for array of string like this func2(['t1', 't1']); this work, but i want to extend the params info? You can use a small helper function like this: type TInfo=record s: string; i: integer; end; function makeinfo(s:string;i:integer):TInfo; begin result.s:=s; result.i:=i; end; ... func1([makeinfo('test1', 1), makeinfo('test2', 2)]); Ludo _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Thomas Young
to Howard Page-Clark: You have to declare the parameter type independently of the function declaration. You can…

Howard are you saying FPC has dynamic arrays? Is it documented? I've been reading the documentation page by page and I've not seen any mention of it. I'm far from completing the reading by the way.

If dynamic arrays are part of FPC I couldn't be more delighted. I don't completely understand what they are exactly. Would I be right in saying they have the advantages of a linked list without the loss of speed that I so enjoy with arrays? Maybe linked lists are slow for me because of the way I wrote the code. The problem I have with arrays is that I often parse postscript vector art files. These files have to be read in a discovery mode approach because I don't know how many paths I will encounter nor know how many vertices will be in a path. As a result arrays are large enough to accommodate the largest path but are way overkill for a simple path.

My hope would be that I could use a dynamic array to hold just the number of vertices I discover in a path.

Thanks for sharing the code in your post. It fascinates me.

I'm eager to learn all I can about about the amazing things that can be done with FPC. To all those who have been building FPC, thank you. While I have been programming in pascal since 1986 almost daily I've relied on the basic features of the language. Time to stretch my mind.

Thomas Young
cell: 330-256-7064
mobile email:

Howard Page-Clark wrote 38 lines
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; _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Thomas Young
to Thomas Young

Forgive me for being in wide-eyed wonder but my goodness this is more than a nice feature. Brilliant. I feel like a child who has played with toy blocks all these years and who now received keys to a ferrari.

Thomas Young
cell: 330-256-7064
mobile email:

Thomas Young wrote 24 lines
Thank you. Thomas Young cell: 330-256-7064 mobile email: On Feb 9, 2012, at 9:41 PM, Reimar Grabowski wrote: > On Thu, 09 Feb 2012 19:34:06 -0500 > Thomas Young wrote: > >> Howard are you saying FPC has dynamic arrays? Is it documented? > http://wiki.freepascal.org/DYNAMIC_ARRAY > > R. > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Sven Barth
to Thomas Young
Thomas Young wrote
Forgive me for being in wide-eyed wonder but my goodness this is more than a nice feature. Brilliant. I feel like a child who has played with toy blocks all these years and who now received keys to a ferrari.

You might get this feeling for often when you use FPC. I'm already developing inside the compiler, but still there is sometimes the feeling "Oh, shiny!".

Btw: Here is the link to the part where dynamic arrays are described in the language reference:
http://www.freepascal.org/docs-html/ref/refse14.html#QQ2-39-50 (which is a nice bed lecture by the way ;) )

Regards,
Sven

Reply by email