0

I am writing a function that receives 3 square matrices in forms of 2D arrays. It should also receive the size. When I compile, I am getting such error! Note that I looked through the internet but could not find a solution

    #include <stdio.h>
    void matadd(int **a,int** b,int** c,int n){
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
    }
int main(){
    int x[4][4]= {
        {0,1,2,3},
        {4,5,6,7},
        {8,9,10,11},
        {12,13,14,15}
    };
    int y[4][4]= {
        {0,1,2,3},
        {4,5,6,7},
        {8,9,10,11},
        {12,13,14,15}
    };
    int z[4][4] = {0};

    matadd(&&x,&&y,&&z,4);
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            printf("%d ", z[i][j]);
        }printf("\n");
    }
    return 0;
}

And the error is here:

matadd.c: In function ‘main’:
matadd.c:26:9: warning: passing argument 1 of ‘matadd’ from incompatible pointer type [-Wincompatible-pointer-types]
  matadd(x,&&y,&&z,4);
         ^
matadd.c:2:6: note: expected ‘int **’ but argument is of type ‘int (*)[4]’
 void matadd(int **a,int** b,int** c,int n){
      ^~~~~~
matadd.c:26:2: error: label ‘z’ used but not defined
  matadd(x,&&y,&&z,4);
  ^~~~~~
matadd.c:26:2: error: label ‘y’ used but not defined

Note that Can't hardcode the size of array. Different times, different dimensions might be used!

Abu-Bakr
  • 408
  • 6
  • 12
  • Also see [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Apr 24 '18 at 06:55
  • Understood that better general answers are provided elsewhere. But, to be helpful, I wrote a fixed-up version of this code example using variable-size arrays. Is there any way to provide that code now? – MichaelsonBritt Apr 24 '18 at 07:26
  • Yes, Could you send it to my e-mail. abubakr0609@gmail.com – Abu-Bakr Apr 24 '18 at 07:27

1 Answers1

-2
#include <stdio.h>
    void matadd(int a[4][4],int b[4][4],int c[4][4],int n){
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
    }
int main(){
    int x[4][4]= {
        {0,1,2,3},
        {4,5,6,7},
        {8,9,10,11},
        {12,13,14,15}
    };
    int y[4][4]= {
        {0,1,2,3},
        {4,5,6,7},
        {8,9,10,11},
        {12,13,14,15}
    };
    int z[4][4] = {0};

    matadd(x,y,z,4);

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

    return 0;
}
kofhearts
  • 3,607
  • 8
  • 46
  • 79