Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!

Passing Multidimensional Arrays as Parameters

the future of starts demands massive productivity

Author: Embarcadero USA

 Technical Information Database

TI1477D.txt - Passing Multidimensional Arrays as Parameters

Category   :Object Pascal
Platform   :All-32Bit
Product    :


Passing an array to a procedure or function is straight 
forward and behaves as expected.  However, passing a 
multi-dimensional array to a function or procedure is 
not handled in the same way. Consider MyArray to be 
defined as:
MyArray: array[1..3, 1..5] of double;

And you want to pass it to a procedure called 
DoSomeThing() defined as:

procedure DoSomeThing(MyArray: array of double);
   showmessage( floattostr( MyArray[1 , 1]));

One might think a simple statement like 
DoSomeThing(MyArray); would do the trick.  
Unfortunately, this is not the case.
The statement DoSomeThing(MyArray); will not compile.
The compiler sees the two data structures involved
as different types - so it will not allow the
statement.  The DoSomeThing() procedure is 
expecting an array of doubles, but the example is 
passing a multi-dimensional array.

Delphi handles multi-dimensional arrays as user 
defined type, so there is no syntax to tell a procedure 
that its parameter(s) are multi-dimensional arrays - 
without declaring a  type.  Creating a type, and using 
this type as the parameter is the correct method to pass 
a multi-dimensional array. We could just pass a pointer 
to the array, but inside the function, we need to typecast 
that pointer.  What type to cast it as is the next issue.
You would have to have the type defined, or declared
identically  in 2 places.  This method just doesn't make

By defining a type, we can change the process to this:
  TMyArray = array[1..3, 1..5] of double;

   MyArray : TMyArray;

procedure DoSomeThing(MyArray: TMyArray);
   showmessage( floattostr( MyArray[1 , 1]));
Now the actual call looks as we expected:


If you want to use the method of passing a pointer, your 
function might look like this:

  PMyArray = ^TMyArray;
  TMyArray = array[1..3, 1..5] of double;

   MyArray : TMyArray;

procedure DoSomeThing(MyArray: PMyArray);
 showmessage ( floattostr( (MyArray[2,3]) ));

Note, under 32 bit version Delphi, you do not need to 
dereference the MyArray variable inside DoSomeThing().
Under older versions you might have to refer to
MyArray as MyArray^.

If you want to pass just a generic pointer, you may not 
be able to use it directly.  You can declare a local 
variable, and use it.  Again, you may need to cast the 
local variable for older versions of PASCAL.  Of course 
this method does offer more flexibility in data usage.

procedure DoSomeThing(MyArray: pointer);
  t : ^TMyArray;
  t := MyArray;
  ShowMessage ( FloatToStr( t[2,3]) );

Regardless, both calls that use a pointer method, will 
look something like:
MyArray[2, 3] := 5.6;


4/1/99 4:35:29 PM

Article originally contributed by

Python GUI Development

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
CodeDelphiIDERAD Studio

Cartoonify Photos With This Easy But Powerful Neural Network


2021 Annual C++ Developer Survey "Lite" by ISOCpp.org - Results summary


CNC Monitor: An Ultimate Platform For Collecting And Digitizing Data From Machines Is Powered By Delphi


Remarkable Application For Ventilation Devices Is Made In Delphi

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.