Search code examples
pointersstructcompiler-errorsmql5

How to assign a pointer to the array of struct in MQL?


I've 3 separate arrays of struct of the same type, for example:

MqlTradeRequest orders1[];
MqlTradeRequest orders2[];
MqlTradeRequest orders3[];

Similar as described for C in here.

Assuming these arrays are populated later on, how I can create a common pointer to one of these?


So I can do something like: orders_ptr = &orders1 or *orders_ptr = orders1.

Here is my non-working code:

MqlTradeRequest orders1[];
MqlTradeRequest orders2[];
MqlTradeRequest orders3[];

enum ORDERS_POOL {
  POOL1,
  POOL2,
  POOL3
};

void start(ORDERS_POOL _pool = POOL1) {
  MqlTradeRequest (*orders_ptr)[]; // Error: Invalid operation use.
  switch (_pool) {
    case POOL1: orders_ptr = &orders1; break; // Error: Invalid array access, class type expected.
    case POOL2: orders_ptr = &orders2; break; // Error: Invalid array access, class type expected.
    case POOL3: orders_ptr = &orders2; break; // Error: Invalid array access, class type expected.
  }
  for (int i = 0; i < ArraySize(orders_ptr); i++) {
    Print(orders_ptr[i].order);
  }
};

And here is another attempt:

  MqlTradeRequest *orders_ptr; // Error: Invalid operation use.
  switch (_pool) {
    case POOL1: *orders_ptr = GetPointer(orders1); break; // Error: Object pointer expected.
    case POOL2: *orders_ptr = GetPointer(orders2); break; // Error: Object pointer expected.
    case POOL3: *orders_ptr = GetPointer(orders2); break; // Error: Object pointer expected.
  }

By different pool I mean something similar as it's done in OrderSelect with its pool argument, but my pools are completely different.

However above code fails with lots of errors which doesn't make any sense, I've included some of them in the comments.

What would be the right approach?

My goal is to assign a pointer to the array of struct, so I can traverse through the selected array.


Solution

  • Seems to me you cannot do it in MQL5.
    You can use pointers for dynamic objects only like classes, for structures it is a static pointer, same as array or primitive, and MqlRates is a structure not a class.
    If you really need such magic - create classes that have an array of Mql-structures in it.