Problem with arrays

 

Can anyone explain why the following code does not seem to work in a custom indicator?

int start()
{
int TestArray[];
TestArray[5]=99;
Print("T: ",TestArray[5]);

return(0);

}


For some reason it just prints "T: 0" as if the value 99 had never been assigned.

What am I doing wrong?

 

If your TestArray is an array and not a buffer you need to declare it with an array size or use ArrayResize to size it after . . . for example:

int TestArray[5];
 

Try this:

int start()
   {
      int TestArray[5];			// declaring a one-deminsional array with five integer elements
      TestArray[4]=99;			// setting the fifth element to 99 (remember: arrays are zero based)
      Print("T: ",TestArray[4]);	// accessing/printing the contents of the fifth element 

      return(0);
   }

You should also review the Array Functions chapter of documentation.

Hope this helps.

 

So I guess you could fill up an array with something like this:

        double newTestArray[5];             // declaring a one-dimensional array with five integer elements
        for (int ix = 0; ix < 5; ix++)      // fill up array 0 through 4 (5 total)
            newTestArray[ix]=Close[ix];	    // fill with the Close price
        for (ix = 0; ix < 5; ix++)          // Print out array 0 through 4 (5 total)
            Print("P: ",newTestArray[ix]);  // Print if out on 5 lines
 
MisterDog:

So I guess you could fill up an array with something like this:

Yes :-)
Reason: