0
votes

*When I call the user defined function, I receive the following error:`

    error: invalid types ‘double[unsigned int]’ for array subscript
       44 |    C[i][j]=0; 
    ...

The user defined code:

void Matrix(const double * A, const double *B, double * C){
    

   for(unsigned int i=0;i < 4;i++)    
    {    
        for(unsigned int j=0;j < 4;j++)    
        {    
            C[i][j]=0;    
                for(unsigned int k=0;k < 4;k++)    
                {    
                    C[i][j]+=A[i][k]*B[k][j];    
                }    
        }    
    }   
    

    
}   

Any help would be really appreciated.

1
C is of type double*. Then C[i] is a double. Then C[i][j] doesn't make sense.Igor Tandetnik
@IgorTandetnik double * C can't be used to define a matrix?BirdANDBird
Well, as far as the C++ compiler is concerned, double* C is a pointer to an array of doubles. If your code wants to interpret it as a matrix, it's up to that code to compute appropriate offsets into that array for each element.Igor Tandetnik
@IgorTandetnik I understand, we are using pointer in C++BirdANDBird

1 Answers

1
votes
const double * A

This is a pointer to a double object.

A[i]

This expression uses the subscript operator to indirect through the pointer. The resulting value is a sibling of the pointed double object in the same array of double objects.

A[i][k]

This uses the subscript operator on the double object that you got from the first subscript operator. The language doesn't have such operator and as such the program is ill-formed. Same applies to your other pointers.