0

Given the following 2d arrays

float KAL_P_X[2][2];
float KAL_P_Y[2][2];
float KAL_P_Z[2][2];

and the function

float kalman(float * P[2][2], float newAngle, float newRate)
{
 P[0][0] = 1.0f;
 P[0][1] = 2.0f;
 P[1][0] = 1.0f;
 P[1][1] = 2.0f;
 return 1.0f;
};

i get the error

cannot convert 'float (*)[2][2]' to 
'float* (*)[2]' for argument '1' to 'float kalman(float* (*)[2], float, float)'

while using

  float Angle.GX =  kalman(&KAL_P_X, Angle.AX, Raw.GX);
  float Angle.GY =  kalman(&KAL_P_Y, Angle.AY, Raw.GY);
  float Angle.GZ =  kalman(&KAL_P_Z, Angle.AZ, Raw.GZ);

So, what am i doing wrong here?

OrElse
  • 9,709
  • 39
  • 140
  • 253
  • @xing It compiles but is this byreference or value? – OrElse Jun 04 '19 at 16:57
  • @xing i am sorry but i do not quite understand. This way the KAL_P_X, KAL_P_Y and KAL_P_Z won't have the same value? I would love to change the value of the argument within the function. That's why i though of using a pointer in argument – OrElse Jun 04 '19 at 17:04
  • 2
    I would recommend reducing the amount of `*` and `&` used by changing the prototype of `kalman` to `float kalman(float P[2][2], float newAngle, float newRate)`, and call it like `float Angle.GX = kalman(KAL_P_X, Angle.AX, Raw.GX);`. Within `kalman`, accesses to `P[i][j]` will access `KAL_P_X[i][j]`. The parameter `float P[2][2]` is automatically adjusted to `float (*P)[2]`. In the function call, `KAL_P_X` is converted to a pointer to its first element of type `float [2]`, so the pointer is of type `float (*)[2]`, which matches the type of `P`. – Ian Abbott Jun 04 '19 at 17:08

0 Answers0