Using Arrays

 

Hi guys, 

 

I know this may seem like a really easy question but I've been struggling with using Arrays. I've write some code with variables which work but now I want to learn to use Arrays as my next code requires me to use an unknown number of variables (expected to be large). I'm hoping Arrays can solve that problem. 

Could someone please help?

I've written the follow, which works, I get the number 4. 

int start()
  {
  
double   Array1[5]= {0,1,2,3,4,5};
   
   Alert(Array1[4]);
     
   return;
  }

 However, I was wondering why the follow doesn't work: (I was expecting to get the number 4 too, however, i get 0)

int start()
  {
double   Array1[];  
   Array1[0]= {0};
   Array1[1]= {1};
   Array1[2]= {2};
   Array1[3]= {3};
   Array1[4]= {4};
      
   
   Alert(Array1[4]);
     
   return;
  }
 
  1. double   Array1[5]= {0,1,2,3,4,5};
    The array has 5 elements, your initialization contains 6. Either use the correct size, correct initialization, or have mql4 compute it.
    double   Array1[]= {0,1,2,3,4,5}; // has 6 elements Array1[4] == 4

  2. double   Array1[]; // Has no elements
    Array1[0]= {0};    // This shouldn't compile because of the braces. 
    Alert(Array1[4]);  // 0 because Array1 has no elements
    This you can NOT do. Array has ZERO length. You must resize arrays:
    double   Array1[];      // Has no elements
    ArrayResize(Array1, 5); // Now has 5 elements
    Array1[0]= 0;
    :
    Array1[4]= 4;
    Alert(Array1[4]); // 4

 
It works now. Thanks WHReoder :)
Reason: