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?

Ryan Hitt
  • 1
  • 3

2 Answers2

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 **.

duskwuff -inactive-
  • 171,163
  • 27
  • 219
  • 269
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

Amin Negm-Awad
  • 15,951
  • 3
  • 33
  • 50
  • 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. – too honest for this site Nov 22 '16 at 19:25
  • I know that. This is, way I called it type-**compatible**, not equivalent. Compatibility and equivalence are not the same. – Amin Negm-Awad Nov 22 '16 at 19:32
  • 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. – too honest for this site Nov 22 '16 at 20:35
  • 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. – Amin Negm-Awad Nov 23 '16 at 04:32
  • 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! – too honest for this site Nov 23 '16 at 11:46