Recent

Author Topic: [SOLVED] how to pass dynamic array by value?  (Read 557 times)

nds

  • New member
  • *
  • Posts: 8
[SOLVED] how to pass dynamic array by value?
« on: June 29, 2022, 09:56:27 am »
Code: Pascal  [Select][+][-]
  1. Procedure arrp(var a: array of string);
  2. Begin
  3. Setlength(a,1);
  4. End;

Type mismatch on Lazarus 2.2.2
« Last Edit: June 29, 2022, 10:46:21 am by nds »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: how to pass dynamic array by value?
« Reply #1 on: June 29, 2022, 09:58:14 am »
Don't just post a fragment, post a complete compilable example.

Try defining a type for a prior to the procedure declaration.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

nds

  • New member
  • *
  • Posts: 8
Re: how to pass dynamic array by value?
« Reply #2 on: June 29, 2022, 10:09:10 am »
Don't just post a fragment, post a complete compilable example.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. procedure arrp(var a: array of string);
  3. begin
  4.  setlength(a,1);
  5.  end;
  6. var a:array of string;
  7. begin
  8.   arrp(a);
  9. end.  
  10.  
Try defining a type for a prior to the procedure declaration.
dynamic array - built-in type

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: how to pass dynamic array by value?
« Reply #3 on: June 29, 2022, 10:14:34 am »
dynamic array - built-in type

"Array of string" is not a built-in type.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

nds

  • New member
  • *
  • Posts: 8
Re: how to pass dynamic array by value?
« Reply #4 on: June 29, 2022, 10:16:05 am »
https://wiki.freepascal.org/Dynamic_array

this code works
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var a:array of string;
  3. begin
  4.    setlength(a,1);
  5. end.
  6.  
« Last Edit: June 29, 2022, 10:21:05 am by nds »

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: how to pass dynamic array by value?
« Reply #5 on: June 29, 2022, 10:23:45 am »
This is not an dynamic array but an open array. The syntax for both is the same, but they are different concepts.

To use a dynamic array as function parameter you need to define the type as an explicitly named type:
Code: Pascal  [Select][+][-]
  1. type TDynStringArray = array of String;
  2. procedure Foo(arr: TDynStringArray);
  3. ...
Some types are already pre-defined for example the generic TArray:
Code: Pascal  [Select][+][-]
  1. procedure Foo(arr: specialize TArray<String>);
  2. ...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11444
  • FPC developer.
Re: how to pass dynamic array by value?
« Reply #6 on: June 29, 2022, 10:24:30 am »
Code: Pascal  [Select][+][-]
  1. Procedure arrp(var a: array of string);
  2. Begin
  3. Setlength(a,1);
  4. End;

Type mismatch on Lazarus 2.2.2

You try to pass a dynamic array to an open array parameter. Despite having roughly the same syntax, they are not the same. To do what you want, define array of <x> as a separate type, or better use the predefined ones for basic types in types uni like https://www.freepascal.org/docs-html/rtl/types/tstringdynarray.html

nds

  • New member
  • *
  • Posts: 8
Re: how to pass dynamic array by value?
« Reply #7 on: June 29, 2022, 10:45:38 am »
Warfley, marcov Thanks

 

TinyPortal © 2005-2018