MQL4 Reference Array Functions ArrayCopy

MQL4 Help as One File:

ArrayCopy

It copies an array into another one.

int  ArrayCopy(
   void&        dst_array[],         // destination array
   const void&  src_array[],         // source array
   int          dst_start=0,         // index starting from which write into destination array
   int          src_start=0,         // first index of a source array
   int          count=WHOLE_ARRAY    // number of elements
   );

Parameters

dst_array[]

[out]  Destination array

src_array[]

[in]  Source array

dst_start=0

[in]  Starting index from the destination array. By default, start index is 0.

src_start=0

[in]  Starting index for the source array. By default, start index is 0.

count=WHOLE_ARRAY

[in]  Number of elements that should be copied. By default, the whole array is copied (count=WHOLE_ARRAY).

Return Value

It returns the number of copied elements.

Note

If count<=0 or count>src_size-src_start, all the remaining array part is copied. Arrays are copied from left to right. For series arrays, the starting position is correctly defined adjusted for copying from left to right. If an array is copied to itself, the result is undefined.

If arrays are of different types, during copying it tries to transform each element of a source array into the type of the destination array. A string array can be copied into a string array only. Array of classes and structures containing objects that require initialization aren't copied. An array of structures can be copied into an array of the same type only.

For static and dynamic arrays (except for class and structure members), the size of a destination array is automatically increased to the amount of copied data (if the latter exceeds the array size).

Example:

void OnStart()
  {
//---
   int src_data[10];
   for (int i=0; i<ArraySize(src_data); i++) src_data[i]=i;
   int dst_data[];
   //--- copy data to dst_data[]
   ArrayCopy(dst_data,src_data,0,0,WHOLE_ARRAY);
   //--- print copied data[]
   PrintFormat("Copied array size=%d",ArraySize(dst_data));
   for (int i=0; i<ArraySize(dst_data); i++) PrintFormat("index=%d, value=%d",i,dst_data[i]);
  }