Array out of range in mql4 build 600

 

Hi my friends,

I don't know how to deal with this problem.

I've written a code by use of Arrays but I don't know why it doesn't work or compile on the graphs. and the most incredible thing is that I was using some other codes look like this but now it doesn't work !!

it print in journal " array out of range in test.mql4 " .

Here is my test code :

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                                    Parham.trader |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Parham.trader"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

int i,j,array[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
//u=test_func(i);
   for(i=0;i<4;i++)
     {
      array[i]=i+1;
      j=array[i];
     }
   Alert(j);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
 
parham.trader: I've written a code by use of Arrays but I don't know why it doesn't work or compile on the graphs. and the most incredible thing is that I was using some other codes look like this but now it doesn't work !! it print in journal " array out of range in test.mql4 " .
int i,j,array[];

Array has no size and you don't resize it.
 
WHRoeder:
Array has no size and you don't resize it.

Isn't it possible to use a dynamic array in cases like this?
 
parham.trader:

Isn't it possible to use a dynamic array in cases like this?

Yes, but when assigning a variable to the array index position, you have to make sure that the index position exists
 
GumRai:

Yes, but when assigning a variable to the array index position, you have to make sure that the index position exists


thank you for your response, So I understood from the answers that the best way for assigning the array's index is to make an array in size of 1 ( for example a[1]) and when I want to add any other index then I resize it (ArrayResize(a,2) ) and add a new array to it also add my variable to the newly added index for example. ( a[1]=1 ).

Am I correct?

 
parham.trader:


thank you for your response, So I understood from the answers that the best way for assigning the array's index is to make an array in size of 1 ( for example a[1]) and when I want to add any other index then I resize it (ArrayResize(a,2) ) and add a new array to it also add my variable to the newly added index for example. ( a[1]=1 ).

Am I correct?


I'm no expert on arrays and I don't understand everything.

But for a dynamic array, declare it as a[] and then resize it to 1

 
string arrayid[]={"arraynum0","arraynum1","arraynum2"};

int arrayidcnt=ArraySize(arrayid);