Board index » delphi » Passing multidimensional arrays to functions/procedures

Passing multidimensional arrays to functions/procedures

   I am having a problem with multidimensional arrays. I want to pass
a multidimensional array to a function that I have written but keep
getting errors when compiling/running. It appears that part of the
problem is in the declaration part of the procedure/function. Whilst
a one dimensional array is easily declared I dont know how to declare
a multidim' array in the header. Here is an example of the code.

procedure TForm1.Button1Click(Sender: TObject);

const a1 : array[1..3] of integer = (0,300,600);
      a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));

begin

    MultArrayTest(a1,a2);

end;

procedure MultArrayTest(array1 : array of integer ; array2 : array of integer);

var n : integer;

begin
  for n := 0 to High(array1) do
      Form1.Canvas.TextOut(array1[n],100,IntToString(array2[n,2]));
end;

I keep getting a 'Type Mismatch' at MultArrayTest(a1,a2)

Any help out there please ?????

 

Re:Passing multidimensional arrays to functions/procedures


Quote
Peter McHardie <p-mchar...@adfa.oz.au> wrote:

>    I am having a problem with multidimensional arrays. I want to pass
> a multidimensional array to a function that I have written but keep
> getting errors when compiling/running. It appears that part of the
> problem is in the declaration part of the procedure/function. Whilst
> a one dimensional array is easily declared I dont know how to declare
> a multidim' array in the header. Here is an example of the code.

> procedure TForm1.Button1Click(Sender: TObject);

> const a1 : array[1..3] of integer = (0,300,600);
>       a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));

> begin

>     MultArrayTest(a1,a2);

> end;

> procedure MultArrayTest(array1 : array of integer ; array2 : array of integer);

> var n : integer;

> begin
>   for n := 0 to High(array1) do
>       Form1.Canvas.TextOut(array1[n],100,IntToString(array2[n,2]));
> end;

> I keep getting a 'Type Mismatch' at MultArrayTest(a1,a2)

> Any help out there please ?????

Declare your own types.

type
        ptrA1 = ^a1;
        ptrA2 = ^a2;
        a1 = array[1..3] of integer;
        a2 = array[1..3,1..3] of integer;

Then pass the pointers.

Lloyd (Borland)

Re:Passing multidimensional arrays to functions/procedures


In <3t9dei$...@druid.borland.com> Lloyd <llinkla...@wpo.borland.com> writes:

Quote
>Peter McHardie <p-mchar...@adfa.oz.au> wrote:

>>    I am having a problem with multidimensional arrays. I want to pass
>> a multidimensional array to a function that I have written but keep
>> getting errors when compiling/running. It appears that part of the
>> problem is in the declaration part of the procedure/function. Whilst
>> a one dimensional array is easily declared I dont know how to declare
>> a multidim' array in the header. Here is an example of the code.

>> procedure TForm1.Button1Click(Sender: TObject);
>> const a1 : array[1..3] of integer = (0,300,600);
>>       a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));
>> begin
>>     MultArrayTest(a1,a2);
>> end;

>> procedure MultArrayTest(array1 : array of integer ; array2 : array of integer);
>> var n : integer;
>> begin
>>   for n := 0 to High(array1) do
>>       Form1.Canvas.TextOut(array1[n],100,IntToString(array2[n,2]));
>> end;

>> I keep getting a 'Type Mismatch' at MultArrayTest(a1,a2)
>> Any help out there please ?????
>Declare your own types.
>type
>    ptrA1 = ^a1;
>    ptrA2 = ^a2;
>    a1 = array[1..3] of integer;
>    a2 = array[1..3,1..3] of integer;
>Then pass the pointers.
>Lloyd (Borland)

Not really a good advice. The called procedure would have no way of
knowing what kind of array the passed pointer was pointing to.

A better solution is to exploit the open array syntax that the original
poster used. The problem with his code is that the passed arrays are of
completly different types.

The two "constant" definitions

Quote
>> const a1 : array[1..3] of integer = (0,300,600);
>>       a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));

declares a1 as an array of integer, while a2 is an array of array of integers
which is something else.

To avoid this use something like this:

type
  TTriple = record
    P : array[1..3] of integer;
  end;
const
  a1 : array[1..1] of TTriple = (P:(0,300,600));
  a2 : array[1..3] of TTriple = (P:(1,1,1), P:(2,2,2), P:(3,3,3));

procedure MultArrayTest(a1, a2: array of TTriple);
var
  i : integer;
  j : integer;
  k : integer;
begin
  for i := Low(a1) to High(a1) do
    for j := Low(a2) to High(a2) do
      for k := 1 to 3 do
        Form1.Canvas.TextOut(a1[i].P[k], 100, IntToString(a2[j].P[k]));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MultArrayTest(a1,a2);
end;

--
 Hallvard Vassbotn  | Falcon AS (a Reuters company) | Without programmers,
 hallv...@falcon.no | Stranden 1, 0250 OSLO, Norway | the world would stop!

Re:Passing multidimensional arrays to functions/procedures


Quote
Peter McHardie (p-mchar...@adfa.oz.au) wrote:

:    I am having a problem with multidimensional arrays. I want to pass
: a multidimensional array to a function that I have written but keep
: getting errors when compiling/running. It appears that part of the
: problem is in the declaration part of the procedure/function. Whilst
: a one dimensional array is easily declared I dont know how to declare
: a multidim' array in the header. Here is an example of the code.

: procedure TForm1.Button1Click(Sender: TObject);

: const a1 : array[1..3] of integer = (0,300,600);
:       a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));

: begin

:     MultArrayTest(a1,a2);

: end;

: procedure MultArrayTest(array1 : array of integer ; array2 : array of integer);

                                                               ^^^^^
This is the problem.  What you want to declare is "array of array of integer".
Unfortunately, Object Pascal does not allow this (probably because it
doesn't have any way to reference the second dimension high limit).

This probably isn't relevant, but VAX Pascal had a very elegant way of
representing this construct :

procedure X (V : Array [LB1..UB1:Integer] of
                   Array[LB2..UB2:Char] of
                     SomeType);

Notice that a) you don't have integer indexes b) you don't have to start
at zero and c) you can have more than one dimension.

This doesn't help, I know, but hey...

Erik Turner
etur...@ccd.harris.com

Re:Passing multidimensional arrays to functions/procedures


Quote
In article <1995Jul3.020315.13...@sserve.cc.adfa.oz.au> Peter McHardie <p-mchar...@adfa.oz.au> writes:
>   I am having a problem with multidimensional arrays. I want to pass
>a multidimensional array to a function that I have written but keep
>getting errors when compiling/running. It appears that part of the
>problem is in the declaration part of the procedure/function. Whilst
>a one dimensional array is easily declared I dont know how to declare
>a multidim' array in the header. Here is an example of the code.
>procedure TForm1.Button1Click(Sender: TObject);
>const a1 : array[1..3] of integer = (0,300,600);
>      a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));
>begin
>    MultArrayTest(a1,a2);
>end;

Before the procedure (or in a Globals unit, etc.) do this:

TYPE
   OneD_array = array[1..3] of integer;
   TwoD_Array =  array[1..3,1..3] of integer;

CONST
    a1 : OneD_Array = (0,300,600);
   a2 : TwoD_Array =  ((1,1,1),(2,2,2),(3,3,3));

and the compiler should be happy.

Re:Passing multidimensional arrays to functions/procedures


If the size of only one of the dimensions of the parameter is unknown,
then this may be one solution :

  TDimOne = array[0..3] of Integer;

  procedure asdfasdf( ....   :array of TDimOne);

Bent Tranberg, Grimstad, Norway

Re:Passing multidimensional arrays to functions/procedures


In article <3t9dei$...@druid.borland.com>, Lloyd <llinkla...@wpo.borland.com> says:

Quote

>Peter McHardie <p-mchar...@adfa.oz.au> wrote:

>>    I am having a problem with multidimensional arrays. [..]

>> const a1 : array[1..3] of integer = (0,300,600);
>>       a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));

>[..]
>> procedure MultArrayTest(array1 : array of integer ; array2 : array of integer);

>Declare your own types.

>type
>        ptrA1 = ^a1;
>        ptrA2 = ^a2;
>        a1 = array[1..3] of integer;
>        a2 = array[1..3,1..3] of integer;

>Then pass the pointers.

>Lloyd (Borland)

Why pass the arrays as pointers explicitely?  Wouldn't it make more sense
(looking at the declaration) to pass the constants as "const" parameters?
Or, if they are to be treated as variables, as "var" parameters?

Eg.
procedure MultArrayTest(const arr1 : a1; const arr2 : a2);
procedure MultArrayTest(var arr1 : a1; var arr2 : a2);

I understand that generally one should be careful to not pass copies of large
variables on the stack (although theses don't even seem that large).  
But, as I understand, internally this compiles to only the pointers being passed,
the overhead should be the same, but, IMHO, it would be much easier to read...

Just my 2 cents worth,

Jochen

________________________________________________________________________
joch...@berlin.snafu.de                        71270,3...@compuserve.com
joc...@cs.umd.edu                              enough already

Re:Passing multidimensional arrays to functions/procedures


Quote
In article <1995Jul3.020315.13...@sserve.cc.adfa.oz.au> Peter McHardie <p-mchar...@adfa.oz.au> writes:
>   I am having a problem with multidimensional arrays. I want to pass
>a multidimensional array to a function that I have written but keep
>getting errors when compiling/running. It appears that part of the
>problem is in the declaration part of the procedure/function. Whilst
>a one dimensional array is easily declared I dont know how to declare
>a multidim' array in the header. Here is an example of the code.

>procedure TForm1.Button1Click(Sender: TObject);

>const a1 : array[1..3] of integer = (0,300,600);
>      a2 : array[1..3,1..3] of integer = ((1,1,1),(2,2,2),(3,3,3));

>begin

>    MultArrayTest(a1,a2);

>end;

>procedure MultArrayTest(array1 : array of integer ; array2 : array of integer);

>var n : integer;

>begin
>  for n := 0 to High(array1) do
>      Form1.Canvas.TextOut(array1[n],100,IntToString(array2[n,2]));
>end;

>I keep getting a 'Type Mismatch' at MultArrayTest(a1,a2)

>Any help out there please ?????

I'm just guessing, and I probably am wrong, but...
put your MultArrayTest procedure BEFORE
your TForm1.Button1Click procedure.

James Richard (JRICH...@TUNS.CA)
"No Assembler Required!"

Other Threads