This is a very elementary problem, but it may be a headache for those who don't know. A function in C + + cannot return an array directly, but an array is actually a pointer, so it can be implemented by a function that returns a pointer. such as a function of matrix multiplication, it is easy for us to write:
1 #include <iostream> 2 3 using namespace std; 4 5 void Multmatrix (float m[4], float a[4], float b[4]) 6 {7 m[0] = a[0]*b[0] + a[1]*b[2]; 8 M[1] = a[0]*b[1] + a[1]*b[3]; 9 m[2] = a[2]*b[0] + a[3]*b[2];10 m[3] = a[2]*b[1] + a[3]*b[3];11 (cout << m[0] << "" << M[1] << endl;13 cout << m[2] << "<< m[3] << endl;14}15 + int main () A[4] = {1.75, 0.66, 0, 1.75};19 float B[4] = {1, 1, 0, 0};20 float *m = new float[4];22 Multmatrix (M, A, B); cout << m[0] << "<< m[1] << endl;25 cout << m[2] <<" "< < m[3] << endl;26 delete[] m;27 return 0;29}
How C + + lets a function return an array