Cannot Declare Array

 
double Limit=Bars-1;
double DR[Limit];

for(i=Bars-1; i>=0; i--)
   {
   DR[i]=Close[i]-Open[i]; // daily return
   Alert("Daily return of bar ", i, " = ",DR[i]);
   }

The above returns the error "invalid index value" in the highlighted position. 

double DR[];
int i;

for(i=Bars-1; i>=0; i--)
   {
   DR[i]=Close[i]-Open[i]; // daily return
   Alert("Daily return of bar ", i, " = ",DR[i]);
   }

The above cannot load in the terminal due to the array being 'out of range'


This is the first time I have had to declare/define a user-defined array so I apologise if this is ultra-basic to the reader, but I can't see what I'm doing wrong. 

In the first situation, I have declared the number of elements (as is shown in the manual: https://book.mql4.com/variables/arrays)

In the second situation, I have left the number of elements undeclared, and 'i' is out of range. How can this be when the value of 'i' is dependant upon Bars-1?

Arrays - Variables - MQL4 Tutorial
Arrays - Variables - MQL4 Tutorial
  • book.mql4.com
A large part of information processed by application programs is contained in arrays. Concept of Arrays Array is an arranged set of values of one-type variables that have a common name. Arrays can be one-dimensional and multidimensional. The maximum admissible amount of dimensions in an array is four. Arrays of any data types are allowed. Array...
 

Either you use a fixed array:

double DR[100];

OR you have to ste the lenght before you use it:

double DR[];
ArraySize(DR,100);
ArrayResize(DR,100);
 
Carl Schreiber:

Either you use a fixed array:

OR you have to ste the lenght before you use it:

Using a fixed array is what I tried in the first box:

double Limit=Bars-1;
double DR[Limit];

for(i=Bars-1; i>=0; i--)
   {
   DR[i]=Close[i]-Open[i]; // daily return
   Alert("Daily return of bar ", i, " = ",DR[i]);
   }

Are you saying that I cannot use a variable in the above, but only a constant?

ArraySize(DR,100);

The above returns the error 'wrong parameters count'

 
It appears that 'ArraySize' simply returns the number of elements in an array, rather than allowing you to define the required number of elements with a variable.
 
koranged:
It appears that 'ArraySize' simply returns the number of elements in an array, rather than allowing you to define the required number of elements with a variable.

Yes, that's correct. ArraySize() does not allocate.

For dynamically allocating an array, you want ArrayResize().

 
koranged:

The above returns the error "invalid index value" in the highlighted position. 

The above cannot load in the terminal due to the array being 'out of range'


This is the first time I have had to declare/define a user-defined array so I apologise if this is ultra-basic to the reader, but I can't see what I'm doing wrong. 

In the first situation, I have declared the number of elements (as is shown in the manual: https://book.mql4.com/variables/arrays)

In the second situation, I have left the number of elements undeclared, and 'i' is out of range. How can this be when the value of 'i' is dependant upon Bars-1?

Could you please post in the right section, don't you see that each time we have to move your topic to mql4 section ?
 
Anthony Garot:

Yes, that's correct. ArraySize() does not allocate.

For dynamically allocating an array, you want ArrayResize().

Thanks.

 
Alain Verleyen:
Could you please post in the right section, don't you see that each time we have to move your topic to mql4 section ?

No I hadn't noticed. 

 
koranged:

No I hadn't noticed. 

Ok, so now you know.
Reason: