1

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 1
    [ out-of-record ] your recent problems seem to be indeed the creative torture-test of the *New*-`MQL4/5` syntax internal boundaries. Good luck on the hunt! Always inspiring the crowd, man +1! – user3666197 Jan 18 '17 at 08:00

3 Answers3

1

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.

user3666197
  • 1
  • 6
  • 50
  • 92
Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
0

As per MQL Support Team reply, it is not possible to create pointers to the struct type objects nor you cannot get the pointer to an array.

Instead it is suggested to wrap MqlTradeRequest array to the class object (CTradeReqArray for example), then it's possible to choose desired array and get the pointer to the CTradeReqArray object, as suggested in Daniel's answer.

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
0

you can use Array of pointers to the objects of the same class like this (having advantage while using native ArrayObj) - and of course, in the class element you can incapsulate your arrays, if you really need) :

//+------------------------------------------------------------------+
//|                                               test_myArrPtrs.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Arrays\ArrayObj.mqh>

class elD  : public CObject
{  
public:    
   double m_name;
   int m_id;  
        
    elD(){};   //Constructor     
};

CArrayObj  arrD;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    elD *el=new elD();
    el.m_name = 1.4000;
    el.m_id = 100;

    arrD.CreateElement(0);
    arrD.Add(el);
    elD *e=arrD.At(0);

    Print(e.m_id);
   arrD.Delete(0);
   
   // only dynamic objects should be deleted
   // if you try to delete a static object then you will obviously get errors. The correct way to delete an object is as follows. 
   if (CheckPointer(el) == POINTER_DYNAMIC)
      delete el;

   if (CheckPointer(e) == POINTER_DYNAMIC)
      delete e;

    return;
  }
//+------------------------------------------------------------------+
JeeyCi
  • 354
  • 2
  • 9