Let us assume the following C (edit only C, not C++) function that performs a matrix-matrix multiplication from two matrices A,B and stores the result in new allocated memory (C):
double **MatrixMult(double **A, int rowA, int colA, double **B, int rowB, int colB)
{
double **C = createEmptyMatrix(rowA, colB);
int i; int j; int k;
for (i=0;i<rowA;i++)
{
for (j=0;j<colB;j++)
{
C[i][j] = 0;
for (k=0;k<rowB;k++)
C[i][j] += (A[i][k]*B[k][j]);
}
}
return C;
}
Matrices A and B are created as follows:
double **A = createEmptyMatrix(10,10);
double **B = createEmptyMatrix(10,10);
with the function
double **createEmptyMatrix(int rows,int cols)
{
int i;
double **L;
L = (double **) malloc(rows*sizeof(double *));
for (i = 0; i < rows; i++){
L[i] = (double *) malloc(cols*sizeof(double));
}
return L;
}
Now, I try to use the SAME function to multiply a constant array (matrix) like
const double A_const[7][9] = {
{1.1,2.1,3.1,4.1,5.1,6.1,7.1,8.1,9.1},
{1.2,2.2,3.2,4.2,5.2,6.2,7.2,8.2,9.2},
{1.3,2.3,3.3,4.3,5.3,6.3,7.3,8.3,9.3},
{1.4,2.4,3.4,4.4,5.4,6.4,7.4,8.4,9.4},
{1.1,2.1,3.1,4.1,5.1,6.1,7.1,8.1,9.1},
{1.2,2.2,3.2,4.2,5.2,6.2,7.2,8.2,9.2},
{1.3,2.3,3.3,4.3,5.3,6.3,7.3,8.3,9.3}
};
with a non constant matrix B (dimension and content of A_const are chosen by random).
Basically, I try to pass a const array of doubles to a function that assumes double**
as an argument.
I would like to avoid changing the definition of the function "MatrixMult". So, is it possible to define a ** (double pointer) to a constant array?