2D Array as function parameters

 

 Hi guys

 

So i want to pass a 2Dimensional array as a parameter.

 

The ArrayPut function should insert an element at the end of an existing array, in this case the parameter is again an array. So it should access a dst_array at a particular index and put the info array in there.

 

The ArrayShift function should remove the first element of an array (which in this case, is also an array because it's 2D)

int ArrayPut(int &dst_array[][], const int &info[])
   {
   ArrayResize(dst_array, ArrayRange(dst_array, 0)+1);
   ArrayCopy(dst_array[ArrayRange(dst_array, 0)], info, 0, 0); (*)(**)
   return ArrayRange(dst_array, 0);
   }
  
int ArrayShift(int &dst_array[][])
   {
   for (int i=0; i < ArrayRange(dst_array, 0); i++)
      dst_array[i] = dst_array[i+1]; (***)(****)
   ArrayResize(dst_array, ArrayRange(dst_array, 0)-2);
   return (ArrayRange(dst_array, 0));
   }


 However it's not working. These are the Errors my compiler gives me:

 

'dst_array' - invalid array access      3stepsellout.mq4        136     14 (*)
'dst_array' - array required    3stepsellout.mq4        136     14 (**)
'dst_array' - invalid array access      3stepsellout.mq4        143     7 (***)
'dst_array' - invalid array access      3stepsellout.mq4        143     22 (****)

I've marked the corresponding lines with stars *. Now, where did i go wrong?


Thanks for your help :)
 

 
Don't use ArrayCopy with a 2D array.
int ArrayPut(int &dst_array[][], const int &info[]){
   int iDst = ArrayRange(dst_array, 0);
   ArrayResize(dst_array, iDst+1);                  // Make room.
   int iSrc = ArrayRange(dst_array, 1);             // Size of info must match!
   for(int i=0; i < iSrc; ++iSrc)                   // \ Copy the
     dst_array[iDst][i] = info[i];                  // / data.
   return iDst+1;                                   // Return new size.
}

int ArrayShift(int &dst_array[][], int iBeg=0){     // Allow removal anywhere. Better name would contain "delete" not "shift"
   int iDst = ArrayRange(dst_array, 0) - 1;         // \ Last
   int iSrc = ArrayRange(dst_array, 1) - 1;         // / element.
   for(; iBeg < iLast; ++i)                         // \  copy
      for(int j=0; j < iSrc; ++j)                   //  > the
         dst_array[iBeg][j] = dst_array[iBeg+1][j]; // /  data.
   ArrayResize(dst_array, iDst);                    // Shrink
   return iDst;                              
}
You probably should not be using a 2D array but an array of a structure. See https://www.mql5.com/en/forum/159649#comment_3866009
 

Thank you, with some minor fixes your script seems to work :)