-2

I'm adding two matrices by calling a function in C but I cannot make it work. Can anyone tell me what is wrong in my code?

//Program to print the sum of 2 matrices
#include<stdio.h>
#include<stdlib.h>
//fucntion to fetch matrix from user
void getMatrix(int** firstMatrix, int row, int col){
   int i,j;
   for(i = 0; i < row ; i++){
      for(j = 0; j < col; j++){
         scanf("%d",firstMatrix[i][j]);
      }
   }

}

int main(int argc, char *argv[]){
   int matrixDim,i,j;
   printf("Enter the dimensions\n");
   scanf("%d",&matrixDim);
   int firstMatrix[matrixDim][matrixDim];
   int secondMatrix[matrixDim][matrixDim];

   printf("Enter the elements of 1st matrix\n");
   getMatrix(firstMatrix,matrixDim,matrixDim);

   printf("Enter the elements of 2nd matrix\n");
   getMatrix(secondMatrix,matrixDim,matrixDim);
   //to print the sum of matrices
   printf("Sum of given matrices is\n");
   for(i = 0; i < matrixDim; i++){
      for(j = 0; j < matrixDim; j++){
         printf("%d\t",firstMatrix[i][j]+secondMatrix[i][j]);
      }
      printf("\n");
   }
   return EXIT_SUCCESS;
}

I'm getting the following warnings: Passing argument 1 of 'getMatrix' from incompatible type.

After running the code, I am not able to read the matrix elements through the function, the program stops working and ends.

veducm
  • 5,933
  • 2
  • 34
  • 40
  • 3
    `int** firstMatrix` - that's a different data structure, and you won't know the size of your Matrix. you *really* should read on how pointers and arrays work in C. it's not complicated, once you grasp the idea. – Karoly Horvath Jan 28 '14 at 14:23
  • Have a look at this answer: http://stackoverflow.com/questions/4051/passing-multidimensional-arrays-as-function-arguments-in-c – mikea Jan 28 '14 at 14:26
  • 1
    http://www.ibiblio.org/pub/languages/fortran/append-c.html , in particular "Why a double pointer can't be used as a 2D array?" – Karoly Horvath Jan 28 '14 at 14:30

2 Answers2

1

By passing a 2D array firstMatrix to your function getMatrix which expects its first parameter as int **, you are assuming that pointers are arrays. No. pointers are not arrays. firstmatrix is not of type int ** , rather it is of type int (*)[matrixDim] (after decay to pointer).
A possible solution is to change your function's parameter int **firstMatrix to int (*firstmatrix)[matrixDim].

void getMatrix(int row, int col, int (*firstMatrix)[row]) { ... }
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Unfortunately, no object designated by the identifier `firstMatrix` is of a pointer-to-array type. The one in the function declaration is a pointer-to-pointer, and the other one is an array of arrays. I'd upvote your answer if you could please fix this. –  Jan 28 '14 at 14:29
  • @H2CO3; Sorry. Fixed that. – haccks Jan 28 '14 at 14:31
  • `matrixDim`? that's a variable. it's not even in scope. – Karoly Horvath Jan 28 '14 at 14:33
  • @KarolyHorvath; What? I do not understand. – haccks Jan 28 '14 at 14:34
1

change Interface to

void getMatrix(int row, int col, int firstMatrix[row][col])

and also change to

scanf("%d", &firstMatrix[i][j]);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70