0

*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.

4
  • 2
    C is of type double*. Then C[i] is a double. Then C[i][j] doesn't make sense. Commented Nov 13, 2020 at 3:57
  • @IgorTandetnik double * C can't be used to define a matrix? Commented Nov 13, 2020 at 3:59
  • 1
    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. Commented Nov 13, 2020 at 4:05
  • @IgorTandetnik I understand, we are using pointer in C++ Commented Nov 13, 2020 at 4:11

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

6 Comments

How to define such loops using double * C ?
@BirdANDBird It's unclear what you mean by "such loops".
C[i][j]+=A[i][k]*B[k][j];
@BirdANDBird You cannot do that with a pointer to double, as I have explained in my answer. Therefore you cannot "define such loops" "using such pointer".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.