If you want it initialized, you must do it.
Wrong. You have been told twice, now three times.
- They are initialized once on program load.
- They don't update unless you assign to them.
- In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants.
There is no default in MT5
(or MT4 with strict
which you should always use.)
MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
- Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
external static variable - Inflation - MQL4 programming forum
I just have got a new problem. hope you guys solve this.
suppose I have this
struct x { int y[]; int z; };
then I declare struct as array
x test[];
If I want to create some function calling test[]
like this
void func (.... &test[])
{
}
this is my question
1.What is prefix or type of &test[]? What type should I fill in this(....)?
2.how to access the member of arraystruct (there are 2 y[] and z) in the function when I pass array with struct ?
thank you again
I just have got a new problem. hope you guys solve this.
suppose I have this
then I declare struct as array
If I want to create some function calling test[]
like this
this is my question
1.What is prefix or type of &test[]? What type should I fill in this(....)?
2.how to access the member of arraystruct (there are 2 y[] and z) in the function when I pass array with struct ?
thank you again
I would suggest this option:
//+------------------------------------------------------------------+ //| Test_en.mq5 | //+------------------------------------------------------------------+ struct Sx { int y[3]; int z; //--- Constructor Sx() { ArrayInitialize(y,1); z=2; } //--- Destructor ~Sx() { } }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { Sx test[]; ArrayResize(test,3); //--- func(test,2); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void func(Sx &struct_array[],int index) { int size=ArraySize(struct_array); if(index>=0 && index<size) { Print(IntegerToString(struct_array[index].y[0])); Print(IntegerToString(struct_array[index].z)); } } //+------------------------------------------------------------------+
result:
2019.10.23 12:19:14.419 Test_en (EURUSD,H1) 1 2019.10.23 12:19:14.419 Test_en (EURUSD,H1) 2
x test[];1.What is prefix or type of &test[]? What type should I fill in this(....)?
2.how to access the member of arraystruct (there are 2 y[] and z) in the function when I pass array with struct ?
- The type of test[n] is x.
- Same as any structure access. test[n].z test[n].y[m].
- Of course unless you give test[] a size, you better not try to access it.
- Of course unless you give a specific test[n].y[] a size, you better not try to access that either.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi experts,
I am confused about these code. I have tried to create array in struct.Those initial value should be "0" in all array.
but why not?How does it come from?
in my opinion, it should be
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
Could anyone please help
thank you