#property strictstruct MyRates
{
datetime time; // Period start timedouble open; // Open pricedouble high; // The highest price of the perioddouble low; // The lowest price of the perioddouble close; // Close pricelong tick_volume; // Tick volumeint spread; // Spreadlong real_volume; // Trade volumeint Bar;
};
//+------------------------------------------------------------------+//| from https://docs.mql4.com/constants/structures/mqlrates//+------------------------------------------------------------------+//// struct MqlRates// {// datetime time; // Period start time// double open; // Open price// double high; // The highest price of the period// double low; // The lowest price of the period// double close; // Close price// long tick_volume; // Tick volume// int spread; // Spread// long real_volume; // Trade volume// }//intOnInit()
{
MqlRates mqRates[1];
int copied = CopyRates(NULL,0,0,1,mqRates);
//+------------------------------------------------------------------+// from https://docs.mql4.com/basis/types/casting#casting_structure//// Typecasting of Simple Structure Types// Data of the simple structures type can be assigned to each other only // if all the members of both structures are of numeric types. In this // case both operands of the assignment operation (left and right) must // be of the structures type. The member-wise casting is not performed, // a simple copying is done. If the structures are of different sizes, // the number of bytes of the smaller size is copied. Thus the absence // of union in MQL4 is compensated.//+------------------------------------------------------------------+//
MyRates mrRates = mqRates[0];
return(0);
}
//+------------------------------------------------------------------+//| Script program start function |//+------------------------------------------------------------------+voidOnStart()
{
//---
}
So why can't a struct with 8 elements be assigned to a struct with the same 8 elements, same numeric data types, along with one additional numeric element?
Here is the code:
Here are the compilation results:
As per the code example at https://docs.mql4.com/basis/types/casting#casting_structure, a struct with 1 element can be assigned to a struct with 2 elements of different numeric data type.
So why can't a struct with 8 elements be assigned to a struct with the same 8 elements, same numeric data types, along with one additional numeric element?