-1

First of all: yes, I know this is a common topic and I've tried several proposed solutions. Nothing worked for me (at least nothing with "nice" code), I finally tried the way described here (2nd variant in jxh's solution): 2D array passing to a function

#include<stdio.h>
typedef double real32;

void Multiply_Matrix_Vector38(int n, int m, real32 A[n][m], real32 B[m], real32 Result[n]);

int main()
{
  real32 A[3][8];
  real32 B[8];
  real32 Result[3][3];
  int i;

  for(i = 0; i < 8; i++) {
    A[0][i] = i;
    A[1][i] = i+1;
    A[2][i] = i+2;
    B[i] = i;
    B[i] = i;
    B[i] = i;
  }

  Multiply_Matrix_Vector(3, 8, A[3][8], B[8], Result[3]);
  return 0;
}

void Multiply_Matrix_Vector(int n, int m, real32 A[n][m], real32 B[m], real32 Result[n])
{
  uint8 i, j;
  real32 sum;

  sum = 0;
  for(i = 0; i < n; i++  {
    for(j = 0; j < m; j++) {
      sum = sum + A[i][j] * B[j];
      printf("A %f\n", A[i][j]);
    }
    Result[i] = sum;
    sum = 0;
  }
}

I still get incompatible type errors - how can this be fixed?

Community
  • 1
  • 1
BThomas
  • 101
  • 4

3 Answers3

1

As arrays decays to pointers when being passed to a function, what's really passed is real32 (*)[8] and real32 *.

You can modify the function appropriately:

void Multiply_Matrix_Vector38(int n, int m, real32 (*A)[m], real32 *B, real32 *Result);

You also calling the function with the wrong arguments. You're calling it with a single value in the arrays, and not the arrays themselves. And remembering that array indexes goes from zero to size minus one, you're also out of bounds.

Instead do just

Multiply_Matrix_Vector(3, 8, A, B, Result);

Oh, and do you really want Result to be an array of arrays too?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Your are not passing the arrays when you do Multiply_Matrix_Vector(3, 8, A[3][8], B[8], Result[3]);

Rather, you are passing the element of A at (3,8), the 9th element of B.

Your call should be the following : Multiply_Matrix_Vector(3, 8, A, B, Result[0]);

The error should have helped you with that : your are passing doubles instead of arrays of doubles.

Result is n array of 3 arrays of size 3, thus Result[0], Result[1], and Result[2] have the correct types. Result[3] is out of bounds however.

Finally your function prototype should be void Multiply_Matrix_Vector(...) not void Multiply_Matrix_Vector38(...)

Cimbali
  • 11,012
  • 1
  • 39
  • 68
1

You missed a couple of things, like the ) in.

for(j = 0; j < m; j++)

Also you should have called

Multiply_Matrix_Vector(3, 8, A, B, Result[3])

Instead of.

Multiply_Matrix_Vector(3, 8, A[3][8], B[8], Result[3])

Because your arguments A[3][8] and B[8] are real32 variables (you are accesing to an element of the matrix and an element of the vector), instead of that you have to use A and B.

And finally, you have to declare.

void Multiply_Matrix_Vector(int n, int m, real32 A[n][m], real32 B[m], real32 Result[n]);

Above the main function to use it there.

Note: Result should be an array, because if multiply A (3x8) with B (8x1), the result should be an array with dimensions 3x1.

Patricio Sard
  • 2,092
  • 3
  • 22
  • 52