Forum > General

multi-dimensional open array parameter?

(1/8) > >>

photor:
According to http://www.freepascal.org/docs-html/ref/refsu64.html, functions in free pascal can use open array parameters, as

--- Code: ---Function Average (Row : Array of integer) : Real;
--- End code ---
to match any size of ONE-DIMENSIONAL arrays.
But if I try to define a function with two-dimensional open array parameter, as

--- Code: ---Function Average (Row : Array of Array of integer) : Real;
--- End code ---
, the compiler will prompt

--- Quote ---Error: Type identifier expected
--- End quote ---
. So, how can I do this?

Blaazen:
You can use dynamic array instead of open:

--- Code: ---type
TArr: array of array of Integer;
procedure Qqq(Arr: TArr);
--- End code ---

photor:

--- Quote from: Blaazen on May 30, 2014, 04:50:58 pm ---You can use dynamic array instead of open:

--- Code: ---type
TArr: array of array of Integer;
procedure Qqq(Arr: TArr);
--- End code ---

--- End quote ---
So, free pascal doesn't originally support multi-dimensional open arrays, am I right?
I have tried dynamic array, as

--- Code: ---type
TArr=array of array of Integer;
var
ar2:array[0..1,0..1] of Integer=((2,-1),(-3,2));
procedure Qqq(Arr: TArr);
--- End code ---
But when I call

--- Code: ---  Qqq(ar2);
--- End code ---
in the program, the compiler said

--- Quote ---Error: Incompatible type for arg no. 1: Got "Array[0..1] Of Array[0..1] Of LongInt", expected "TArr"

--- End quote ---
. Any ideas?

JuhaManninen:

--- Quote from: photor on May 31, 2014, 04:20:58 am ---. Any ideas?

--- End quote ---

The type of ar2 must also be TArr. Pascal is strict with types. It is good idea to always define and use new types for such arrays.

photor:

--- Quote from: JuhaManninen on May 31, 2014, 09:37:02 am ---
--- Quote from: photor on May 31, 2014, 04:20:58 am ---. Any ideas?

--- End quote ---

The type of ar2 must also be TArr. Pascal is strict with types. It is good idea to always define and use new types for such arrays.

--- End quote ---
Maybe I haven't expressed myself clearly enough. I want to write a procedure to deal with ar2 (a constant matrix with undetermined size), but I have no idea how to pass ar2 to that procedure as a parameter. Any suggestions?