how to return a matrix?
-
hey
i try to return a matrix from a function I have tried many ways unfortunately it doesn’t work there is my code:
float** toMatrix() {
float mat[3][3]; float a ; float b ; float c ; float d ;a=R; b=I; c=J; d=K;
mat[0][0]= a*a+b*b-c*c-d*d; mat[0][1]= 2*(b*c-a*d); mat[0][2]= 2*(a*c+b*d);
mat[1][0]= 2*(b*c+a*d); mat[1][1]= a*a-b*b+c*c-d*d; mat[1][2]=2*(c*d-a*b);
mat[2][0]= 2*(b*d-a*c); mat[2][1]=2*(a*b+c*d); mat[2][2]=a*a-b*b-c*c+d*d;
return mat;
}
and i have this when i try to compile " error: cannot convert 'float (*)[3]' to 'float**' in return
return mat;"
have anyone an idea of how can i do this?
thanks
^
-
std::array<std::array<float, 3>, 3> mat;
Also use this type as the return type. Or you could write a matrix class and use that.
Even if your code would work like you inteded to, it would be undefined behaviour.
-
In general: A pointer is not an array and an array is not a pointer.
Corollary: A pointer to a pointer is not a pointer to an array and neither of those is a 2D-array, and vice versa.
This is not just a syntactic difference. The actual data layout in memory is totally different.
But, as patrick246 explained, it is unusual to work with raw pointers and arrays in C++ anyway.