Pass Rates as Array entries in .dll C++

 

Hi Guys,

I am parsing a series of rates and then copying the .Open part into a new array, this is done via:

   MqlRates rates[];
   double copyrates[100];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,100,rates);
   
   for (int i=0;i<=(copied-1);i++)  //0 to Copied-1 means that you have 100 elements. 0 to Copied means 101 and hence its an error of bounds.
      {
         copyrates[i]=rates[i].open;
      }

The idea now is to pass the entire copyrates array into a C++ program complied as a .dll. The C++ routine is simple and is just generating the sum of the components of the array. This is how I have implement it:

double std_est(double p[])
{
        int z = sizeof(p);
        int sum = 0;
        for (int i=0; i<=z; ++i)
        {
                sum = sum + p[i];
        }
        return(sum);

}

I then declare the function at the Import section of the MQL code via:

double std_est(double & p[]);

and then try to call it (and there is the problem) via:

Alert("The sum is: ",std_est(copyrates));

The result I am getting is:

The sum is: 5.0

Anyone has any idea what is going on here?

Many thanks for the interest Guys!

 
  1. Instead of CopyRates and then copying the open you could just use CopyOpen - MQL4 Documentation
  2. int z = sizeof(p);
    for (int i=0; i<=z; ++i)
    This means you access one past the end.
  3. for (int i=0;i<=(copied-1);i++)
    for (int i=0;i<  copied   ;i++) // Use the C style [0 .. N)

  4. How can sizeof(p) possibly work? It's a compile time statement; pass the length.



 
WHRoeder:
  1. Instead of CopyRates and then copying the open you could just use CopyOpen - MQL4 Documentation
  2. This means you access one past the end.

  3. How can sizeof(p) possibly work? It's a compile time statement; pass the length.



Thanks for the above WHRoeder. I have made the relevant adjustment however, even by explicitly setting the length in the C function, this doesn't work. It appears that this is looping through a count to 100 (array size) and the output is coming out 100. I am sure that Copyrates holds values cause I run stop checks with Mqlrates Open.