# Multiplying 2 2-dimensional matrices

• 01-18-2012
kevinstrijbos
Multiplying 2 2-dimensional matrices
Hello,

I'm trying to write a simple program which multiplies 2 2-dimensional matrices. I get no compiler errors nor warnings, also with debugging my pointer values seem to be okay, but the problem is that it prints the adresses, anyone got an idea?
This is my code:
Code:

```/* AUTH: Kevin Strijbos  DATE: 18/01/2012   DESCR: multiplies 2 2-dimensional matrices */ #include <stdio.h> void mul_Matrices (int *m1, int *m2, int *result); int main (void) {         int m1[2][2] = {{1, 2},{1, 3}};         int m2[2][2] = {{3, 5},{4, 2}};         int result[2][2];         int counter, smallCounter, index;         index = 0;         mul_Matrices(m1, m2, result);         printf("Result:\n");         for (counter = 0; counter < 2 ;counter++)         {                 for (smallCounter = 0; smallCounter < 2 ;smallCounter++)                 {                         printf("%d\t", result[index]);                         index++;                 }                 printf("\n");         } } /* function that multiplies 2 2-dimensional arrays */ void mul_Matrices (int *m1, int *m2, int *result) {         int counter, smallCounter;         for (counter = 0; counter < 2 ;counter++)         {                 for (smallCounter = 0; smallCounter < 2 ;smallCounter++)                 {                         *result++ = (*m1 * *m2++) + (*(m1+1) * *m2++); // formula for matrix multiplation                 }                 m1 += 2;                 m2 -= 4;         } }```
And this is my output:

Result:
3800320 3800328
3800336 3800344
Press any key to continue . . .
• 01-18-2012
jimblumberg
I don't know why you don't get any errors, but here are the errors my compiler produced:

Quote:

main.c||In function ‘main’:|
main.c|22|error: passing argument 1 of ‘mul_Matrices’ from incompatible pointer type|
main.c|8|note: expected ‘int *’ but argument is of type ‘int (*)[2]’|
main.c|22|error: passing argument 2 of ‘mul_Matrices’ from incompatible pointer type|
main.c|8|note: expected ‘int *’ but argument is of type ‘int (*)[2]’|
main.c|22|error: passing argument 3 of ‘mul_Matrices’ from incompatible pointer type|
main.c|8|note: expected ‘int *’ but argument is of type ‘int (*)[2]’|
main.c|32|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’|
main.c||In function ‘mul_Matrices’:|
main.c|50|warning: operation on ‘m2’ may be undefined|
||=== Build finished: 6 errors, 2 warnings ===|
Jim
• 01-18-2012
kevinstrijbos
Well, that's what happened yesterday also. I don't get any warnings nor errors, but others do?
• 01-18-2012
jimblumberg
What compiler are you trying to use?

Jim
• 01-18-2012
kevinstrijbos
The compiler in microsoft visual studio, think it's GCC?
• 01-18-2012
bernt
If you're going to use result like it's a one-dimensional array, then define it as such. Otherwise you should access the array like it's actually two dimensional: with a row and column index.
• 01-18-2012
jimblumberg
Quote:

The compiler in microsoft visual studio, think it's GCC?
No that would be Microsoft Visual C.

Are you compiling through the IDE or trying to use the command line?

Jim
• 01-18-2012
kevinstrijbos
bernt: doesn't matter, arrays are row-oriented in the memory. So I can use my result array like this, don't need to give a row and column inde.

jim: I'm compiling through the IDE
• 01-18-2012
oogabooga
Quote:

doesn't matter, arrays are row-oriented in the memory. So I can use my result array like this, don't need to give a row and column inde
And yet it doesn't quite work, does it.
• 01-18-2012
jimblumberg
You need to check your compiler settings. You need to insure warnings are being generated, and that you have selected the highest level (w4). Your program has quite a few errors that your compiler should be telling you about.
The following line:
Code:

`            *result++ = (*m1 * *m2++) + (*(m1+1) * *m2++);`
Causes undefined behavior, you can not increment a variable twice in the same sequence point (m2).

As others have said, you have multidimensional arrays, use array notation[][] to access the elements.

Jim