3

I have many constants of 2-dimensional array with one dimension variable in length, like this :

const
  ThiamineRDA: array[0..2, 0..3] of Double =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3), (0, 1, 3, 0.5));
  RiboflavinRDA: array[0..1, 0..3] of Double =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3));

And I want to pass this constants as a parameter to a procedure:

  LoadIntakes(Item, ThiamineRDA);

But how cand I define that procedure to permit that parameter ?

procedure LoadIntakes(Item:PNutrientInfo; IntakesList: array of... ???? );
begin
 //.....
end;
7
  • Do all your arrays have 4 elements as the second parameter? Commented Jun 13, 2018 at 13:20
  • Passing by value will copy the array which could be expensive Commented Jun 13, 2018 at 13:32
  • @Dsm, yes the second dimension is fixed to 4 elements. And your answer it's working. Thanks ! Commented Jun 13, 2018 at 14:43
  • @DavidHeffernan in Dsm's example the array is passed by value too ? I'm not good at this stuff... Commented Jun 13, 2018 at 14:45
  • 1
    Yes, it's a bad idea to pass arrays by value. Don't do it. Commented Jun 13, 2018 at 14:51

2 Answers 2

3

You can't pass open arrays that are open in 2 dimensions. But if one of the dimensions is fixed in size, you can do it, like this:

(I omitted your first parameter so that I could check that it does compile)

type
  TQArray = array[0..3] of double;
const
  ThiamineRDA: array[0..2] of TQArray =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3), (0, 1, 3, 0.5));
  RiboflavinRDA: array[0..1] of TQArray =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3));

procedure LoadIntakes( IntakesList: array of TQArray );
begin
 //.....
end;

procedure Test;
begin
  LoadIntakes( ThiamineRDA );
end;
Sign up to request clarification or add additional context in comments.

2 Comments

Wrong. You can define type for multidimensional array so that you can pass it as parameter to some procedure/function quite similar to your approach. Take a look at my answer
@SilverWarior, I didn't say you can't pass multidimensional arrays, I said that you can't pass an array that is open (unbounded) in 2 dimensions. You can pass a dynamic array of dynamic arrays, which is similar, but you can't then define the constants in a compatible way AFAIK.
-2

if you wish to pass multidimensional open arrays as parameter to some procedure you first need to define special type for such array. You can then even define constants arrays of that type.

So your code would look like this:

type
  ThiamineRDA = array[0..2, 0..3] of Double;
  RiboflavinRDA = array[0..1, 0..3] of Double;

...

const
  ArThiamineRDA: ThiamineRDA =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3), (0, 1, 3, 0.5));
  ArRiboflavinRDA: RiboflavinRDA =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3));

...

  LoadIntakes(Item, ArThiamineRDA);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.