Download MetaTrader 5

copying of structure

To add comments, please log in or register
williamwong
632
williamwong  

Does anyone know what is the command to copy from one structure to another structure?

Alexey Da
Moderator
6625
Alexey Da  
williamwong:

Does anyone know what is the command to copy from one structure to another structure?

Have you read this topic (https://www.mql5.com/en/docs/basis/types/classes)?

It depends on structure which you want to copy.

Structures that do not contain strings or objects of dynamic arrays are called simple structures; variables of such structures can be freely copied to each other, even if they are different structures. Variables of simple structures, as well as their array can be passed as parameters to functions imported from DLL.

Documentation on MQL5: Language Basics / Data Types / Structures and Classes
  • www.mql5.com
Language Basics / Data Types / Structures and Classes - Documentation on MQL5
williamwong
632
williamwong  
alexvd:

Have you read this topic (https://www.mql5.com/en/docs/basis/types/classes)?

It depends on structure which you want to copy.


I was more looking at a function to copy structures of same type without multiple assignment statement of each element in the structure.  Sort of like memory copy in C.

 For example,

struct newStruct { int x; double y};

newStruct A, B;

memcpy(B, A, sizeof(A)); // copy A to B 

Alexey Da
Moderator
6625
Alexey Da  
williamwong:

I was more looking at a function to copy structures of same type without multiple assignment statement of each element in the structure.  Sort of like memory copy in C.

 For example,

struct newStruct { int x; double y};

newStruct A, B;

memcpy(B, A, sizeof(A)); // copy A to B 

You can do it easier.

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
struct newStruct
  {
   int               x;
   double            y;
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnStart()
  {
   newStruct A={2,2};
   newStruct B;
   B=A;
//---
   return(0);
  }
williamwong
632
williamwong  
alexvd:

You can do it easier.

Oh, I didnt know an assignment statement copies one structure to another.  how about structure involving string like mqlparam?

MqlParam a;

MqlParam b;

can I do a=b?  will b.string_value be copied too? 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Structure of Input Parameters
  • www.mql5.com
Standard Constants, Enumerations and Structures / Data Structures / Structure of Input Parameters - Documentation on MQL5
Alexey Da
Moderator
6625
Alexey Da  
williamwong:

how about structure involving string like mqlparam?


It isn't simple structure (https://www.mql5.com/en/docs/basis/types/classes), so you can't. You will get an error if you try.

'b' - structure have objects and cannot be copied
Documentation on MQL5: Language Basics / Data Types / Structures and Classes
  • www.mql5.com
Language Basics / Data Types / Structures and Classes - Documentation on MQL5
To add comments, please log in or register