0

so I'm asked to make the following function:

int **multiplyM(int MA[][], int MB[][], int n, int m)

Which will multiply two matrices. The first one (MA) with dimensions n, n, and the second one (MB) with dimensions n, m. I have everything done with the program, but I get an error caused by the function itself, which says:

"array type has incomplete element type"

I know I can fix it by changing stuff in the function (like changing it to **MA and **MB), but the thing is, I'm not supposed to do that, because I'm supposed to make my program based on this function that was given to me.

So my question is: Is there a way to make this work WITHOUT changing the function?

P0W
  • 46,614
  • 9
  • 72
  • 119
  • 2
    You must declare the size of the last dimension of the array. For example `int **multiplyM(int MA[][N], int MB[][M], int n, int m)` where `N` and `M` are constants. – Murilo Vasconcelos Oct 24 '13 at 19:13
  • 2
    Could you add the code of the function? – Giuseppe Pes Oct 24 '13 at 19:17
  • In c you need to specify the last dimension of array. – Arpit Oct 24 '13 at 19:26
  • 1
    No the last! The only dimension which can be omitted from a function declaration is the first. All the other dimensions *must* be specified. To prove this, try to compute the address of `array[3][4][6]` without knowing the size of the second column. :D – Giuseppe Pes Oct 24 '13 at 19:46

2 Answers2

0

The second dimension must be given for MA and MB

So,

#define SIZE_M 5 //Any constant
#define SIZE_N 6

int **multiplyM(int MA[][SIZE_M], int MB[][SIZE_N], int n, int m)
       //Fix ->           ^^^                ^^^
P0W
  • 46,614
  • 9
  • 72
  • 119
0

You cannot pass a multidimensional array to a function as you are doing. You need to specify the size of the the second dimension (and any further dimension) when declaring the function. Specifying the size here is important. If it were not mandatory, the compiler won't be able to deal with expression such Array[2][3]. The value used as array dimension must be a constant for ANSI C an other versions, but it can be a variable for C99 and successive versions. The C99 standard introduced the variable-length arrays feature, which allows to determine the size of an array at run-time.

So:

#define N 10
#define M 5 

int **multiplyM(int MA[][N], int MB[][M], int n, int m)

I know I can fix it by changing stuff in the function (like changing it to **MA and **MB), but the thing is, I'm not supposed to do that, because I'm supposed to make my program based on this function that was given to me.

Without modifying at least the declaration of the function, you are not going to solve this problem.

Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90