array out of range problem

 

Hi guys,

I am having a bizarre problem. A simple expression is giving me a consistent error. Could anyone give me a hint what is wrong?


//--- preprocessor

.

.

.

int     A[];


//---- in OnInint() section

{

A[0]=5;

Print(A[0]);

}


ERROR: array out of range


Thanks a lot,

 

maybe 

int     A[1];

 

Read in the reference about arrays - their size has to be set initially!!

 

You are trying to fill an array with no elements. You can either declare a static array with a set array size

int arr[1];

arr[0] = 5;

or dynamic array with initialization list or resize

int arr[];
ArrayResize(arr,1);
arr[0] = 5;

OR

int arr[] = {1,2,3,4,5};

or you can use one of the collections from the standard lib

#include<Arrays\ArrayInt.mqh>

CArrayInt arr;

arr.Add(5);
 

Guys,

thank y'all for your feedback. I guess I didn't express the problem clearly. I am trying to define a dynamic integer array and I'm assigning values to it in a for loop. For some reason the array sizes dont match so I get this array out of range error. What I was trying to do was to check the assignment process was completed at each iteration to find which iteration is giving me the out of range error. However, weird thing is, it doesn't ran even the first iteration. I was wondering if that is what happens in these cases by default.


thanks alot

 
You'll find everything you need to solve you problem in the reference (Editor, F1)!
 
Carl Schreiber:
You'll find everything you need to solve you problem in the reference (Editor, F1)!
well thanks for nothing. I know that! and trust me, i wouldn't bother people if i hadn't spend a ridiculous amount of time on it already. Instead of your comment, be a helpful person and help me. I need to create a dynamic array and fill it with numbers. Why doesnt the program let me to put values in the dynamic array?? what on earth is wrong??
 
nicholishen:

You are trying to fill an array with no elements. You can either declare a static array with a set array size

or dynamic array with initialization list or resize

or you can use one of the collections from the standard lib

Thanks a lot for your input. Can I just create a dynamic array and fill it with numbers later on in the code?

int    A[];

for (int i=0;i==50;i++)

     {A[i]=i;}

 
NeeZo_NeeZO:

Thanks a lot for your input. Can I just create a dynamic array and fill it with numbers later on in the code?

int    A[];

for (int i=0;i==50;i++)

     {A[i]=i;}

Yes and no... For that style you will need to use a vector object not array. See my last example with CArrayInt
 
NeeZo_NeeZO:
well thanks for nothing. I know that! and trust me,

Really? So why don't just modify this example  of ArrayGetAsSeries?

...
for(int i=start_index;i<rates_total;i++) 
      buffer[i]=MathAbs(first[i]-second[i]); 
...

i wouldn't bother people if i hadn't spend a ridiculous amount of time on it already.

Just start from the fact that almost no question hasn't answered several times either here (search at the corner top, right) or elsewhere (google)!!

Instead of your comment, be a helpful person and help me.


I wan to show you a faster way to find an answer!

Your first post was was 24 h ago, the answer in the reference took me less than 2 minutes to find!

I need to create a dynamic array and fill it with numbers. Why doesnt the program let me to put values in the dynamic array?? what on earth is wrong??

It does and F1 is a very good way to find an answer!

 
NeeZo_NeeZO:

Thanks a lot for your input. Can I just create a dynamic array and fill it with numbers later on in the code?

int    A[];

for (int i=0;i==50;i++)

     {A[i]=i;}


You've been given the answer by nicholsen in the 3rd post!