Please help: The following compiles in MT4 but not MT5: void demofunc( double & arr[][]) { }

 

Hello,

The following function compiles in MT4 (build 1045) (per @whroeder1 ), but not in MT5 (build 1545):

void demofunc( double & arr[][])
{
}

Does anybody else know why this would compile in MT4 but not in MT5?

What is the correct syntax for defining a function in MT5 that accepts as a parameter a two-dimensional array with both dimension lengths unknown, or is it even possible?

Note that I would like this function to accept a two-dimensional array whose second dimension length could be any value, so changing the code above to arr[][10] instead of arr[][] wouldn't work for me.

I asked this question yesterday in an old thread that seemed the most relevant ( https://www.mql5.com/en/forum/87545 ), but there's been no solution proposed for MT5, so I am posting this question in it's own thread.

Thank you very much in advance,

Robox

how to pass a multidimensional array as a parameter
how to pass a multidimensional array as a parameter
  • www.mql5.com
The code below is not working. Can I pass a multidimensional array as a parameter...
 
You can't use this syntax for multidimensional array parameters in MT5. Second dimension must be specified. Otherwise, use classes or array of structs with arrays.
 
Stanislav Korotky:
You can't use this syntax for multidimensional array parameters in MT5. Second dimension must be specified. Otherwise, use classes or array of structs with arrays.


Yep.

struct my_struct
  {
   double second_dimension[];
  };

void OnStart()
  {
   my_struct first_dimension[2];
   ArrayResize(first_dimension[0].second_dimension,3);
   first_dimension[0].second_dimension[0]=0;
   first_dimension[0].second_dimension[1]=1;
   first_dimension[0].second_dimension[2]=2;

   ArrayResize(first_dimension[1].second_dimension,2);
   first_dimension[1].second_dimension[0]=3;
   first_dimension[1].second_dimension[1]=4;
   
   demofunc(first_dimension);
  }

void demofunc(my_struct& first_dimension[])
  {
   for(int f=0; f<ArraySize(first_dimension); f++)
      for(int s=0; s<ArraySize(first_dimension[f].second_dimension); s++)
         printf("[%i][%i] = %f",f,s,first_dimension[f].second_dimension[s]);
  }
 
honest_knave:


Yep.

Thank you @Stanislav Korotky and @honest_knave for the excellent and definitive answers.

Thank you @honest_knave for such a great solution.  It compiles perfectly in MT5.

I'm always impressed with the brainpower and quick responses on this forum.

Thanks again,

Robox

 

Multi-dimensional arrays are very poor and almost useless with mql5.

 
Alain Verleyen:

Multi-dimensional arrays are very poor and almost useless with mql5.

Oddly enough ArraySort lets you pass multi dimensional arrays and still works correctly on MQL5.

But you can't use (void &array[]) as user defined parameters of course

 
James Cater:

Oddly enough ArraySort lets you pass multi dimensional arrays and still works correctly on MQL5.

Tell me how to sort a multi-dimensional array in DESCENDING order with ArraySort() ?

But you can't use (void &array[]) as user defined parameters of course

Yes that's why I said almost useless, as they need to be global or hard-coded, nothing really dynamic.
 
Alain Verleyen:
Tell me how to sort a multi-dimensional array in DESCENDING order with ArraySort() ?
Normally you use arrays to interate through them. If you need a sorted array in descending order, do ArraySort and then just iterate in reverse order, from n-1 to 0-th index.
 
Stanislav Korotky If you need a sorted array in descending order, do ArraySort and then just iterate in reverse order, from n-1 to 0-th index.
ArraySort only works for numerics. If you have an array of a struct, define your own comparison and use Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum
 
Stanislav Korotky:
Normally you use arrays to interate through them. If you need a sorted array in descending order, do ArraySort and then just iterate in reverse order, from n-1 to 0-th index.
Yeah for sure I know you can iterate in reverse order. That's not an answer to my question ;-)
 
Alain Verleyen:
Yeah for sure I know you can iterate in reverse order. That's not an answer to my question ;-)
Well, then what answer would you mean to get? Probably OP has no need in descending order sorting. Apart from this ArraySort works correctly.
Reason: