How to reuse the same array, but starting at another index in mql5?

36 views Asked by At

In C / C++, you can make an "array view" using a pointer like this :

int array[100];
int * ptr = &array[50]; // note that array + 50 would work too
printf("%d", ptr[3]);

How could you achieve the same in mql5 without copying the full array ? (This is to pass to functions like SocketSend() )

2

There are 2 answers

0
Bishow Gurung On

I don't think we can create pointer in mql5. However, For Array View you can do something like this.

int myArray[5] = {1, 2, 3, 4, 5};
ArrayPrint(myArray, 0, ",", 0, 2); // 0: starting index and 2: element count
//Prints 1,2
0
joserogerio84 On

You pass the array by reference, the function SocketSend is prepared for it.

If you only want to pass a part of the array, then you must make a copy.

A example of passing by reference:

void OnStart(){
   int arr[5]= {1, 2, 3, 4, 5};
   
   PrintArrayExample(arr);
}
void PrintArrayExample(int& a[]){
   for(int i=0; i < ::ArraySize(a); i++)
      Print(a[i]);
}