Dynamic Arrays, can not place data in it

 

Hi all

I'm trying to use dynamic arrays for the first time but I can't seem to place data in them.

I've tried the simplest of approach to see what i'm doing wrong.

Basically in the below example I'm trying to fill in the array with 5 numbers that are exactly as the counter 'x'

but i'm getting out of range.

If instead of [] I place [5] and declare an array of 5 slots it will work just fine


   int dynamic_array[];

   for(int x=0; x<5; x++)
     {
      dynamic_array[x] = x;
      Print("Value is " + IntegerToString(dynamic_array[x]));
     }


thanks in advance

 

Of course, you can't. There is no such thing as a dynamic array in MTx. The array has zero size, so you can't store anything.

Perhaps you should read the manual. ArrayResize - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
   How To Ask Questions The Smart Way. 2004
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

 
William Roeder:

Of course, you can't. There is no such thing as a dynamic array in MTx. The array has zero size, so you can't store anything.

Perhaps you should read the manual. ArrayResize - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
   How To Ask Questions The Smart Way. 2004
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

Yes I've read that already but if I knew the array size before hand why would I want to create it a dynamic?...

shouldnt dynamic array be resized as needed throughout the filling out process?

 
zelda_lee:

Yes I've read that already but if I knew the array size before hand why would I want to create it a dynamic?...

shouldnt dynamic array be resized as needed throughout the filling out process?

Correct, but it does not happen automatically like in other (interpreter) languages. You have to resize them yourself according your needs.

 
Enrique Dangeroux:

Correct, but it does not happen automatically like in other (interpreter) languages. You have to resize them yourself according your needs.

i see

thanks for clarifying

Reason: