0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void adder(int **matrix1, int **matrix2, int **matrix3) {

  for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
      matrix3[i][j] = matrix1[i][j] + matrix2[i][j];
    }
  }

}

int main() {

int matrix1[3][3] = {{1,1,1},{1,1,1},{1,1,1}};
int matrix2[3][3] = {{2,2,2},{2,2,2},{2,2,2}};
int matrix3[3][3];

adder(matrix1, matrix2, matrix3);

for(int i = 0; i < 3; i++){
  for(int j = 0; j < 3; j++){
    printf("%d\t", matrix3[i][j]);
  }
  printf("\n");
}
}

When doing this there are no errors but warning saying "passing argument from incompatible pointer". I've tried working with the adder inputs as pointers using int *matrix1 but this doesn't work either.

What can I do?

7
  • 1
    replace int **matrix1 with int matrix1[3][3], do the same for the other arguments. Or allocate the matrix1 using malloc Commented Nov 22, 2016 at 19:08
  • 1
    void adder(int (*matrix1)[3], int (*matrix2)[3], int (*matrix3)[3]) { Commented Nov 22, 2016 at 19:09
  • Maybe question#5329107 might help Commented Nov 22, 2016 at 19:10
  • Yeah this works but the assignment is to specifically use pointers. Commented Nov 22, 2016 at 19:12
  • 1
    Possible duplicate of How to pass 2d array to a function? Commented Nov 22, 2016 at 19:24

2 Answers 2

1

int ** is not the same type as int[3][3].

int ** is (as used here) a pointer to an array of int * pointers, where each of those pointers addresses an array of ints. In graphical form:

            +-------+     +-----+
int **a --> | int * ----> | int |
            | int * |     | int |
            | int * |     | int |
            +-------+     +-----+

int[3][3] is a single array of 9 ints, which is intended to be accessed as a 3x3 array. A variable of this type can be treated as an int *, but the array that points to behaves as a single pointer, not a double pointer. It doesn't have the extra level of indirection seen in int **.

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

Comments

0

Even an array is type-compatible with a pointer, this does not work for multi-dimensional arrays. You only can convert the outermost dimension. To make a long story short:

  • This would lose the information about size. For the outermost this is okay, because it always have a linear layout. But the inner arrays would lose its size.

  • A pointer to pointer to an object needs at least two pointers. This is obvious. A mutli-dimensional array does not have additional pointer in its inside.

There are some solutions:

Make the type of the parameter

  • a pointer to the inner array (dimensions-1): int (*m)[3]

  • an array, whose outer dimension is incomplete: int m[][3]

  • the real dimension: int m[3][3]

  • or – in your case – make it a simple pointer to the int and iterate over an 1-dimensional array, what will work in your case: int *m

5 Comments

Arrays are not type-compatible with a pointer! Try sizeof(int[9]) == sizeof(int *). And as a multidimensional array is just a 1D array of an array, there is no exception for those.
I know that. This is, way I called it type-compatible, not equivalent. Compatibility and equivalence are not the same.
They are definitively also not type-compatible (if they were, they' s also be equivalent!). That's exactly the reason why int [][] cannot be converted to int **. There is already enough confusion by beginners and such wrong statements just make it worse.
No, that's not the reason. The reason, that [][] cannot be converted to ** and vice versa is, that the objects are different as explained in my answer. Maybe you misunderstand the term compatible. Compatibility and equivalence are still different things. This is the reason, why there are two different terms for it.
The term compatible types is exactly defined in the standard! I don't missunderstand it. Don't try to enforce your very own definition of such well-defined terms!

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.