1

In my main I call this function add like this:

add(a,b,c,row,col);

Its definition is given below:

void add(int **a,int **b,int **c,int row,int col)
{

    int i,j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            c[i][j]=a[i][j]*b[i][j];
        }
    }
}

The main function:.......

int main()
{
    int c[5][5],i,j;
    int **f; 
    int a[5][5],b[5][5],row,col; 
    printf("Enter row : ");
    scanf("%d",&row); 
    printf("Enter column : ");
    scanf("%d",&col);
    printf("Enter matrix A :\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter matrix B :\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }


    add(a,b,c,row,col);  

    printf("Addition :\n");

    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d\t",c[i][j]);
        }
        printf("\n");
    }
    getch();
    return 0;
}

The errors given by compilers:

||warning: command line option '-Wzero-as-null-pointer-constant' is valid for C++/ObjC++ but not for C [enabled by default]|
C:\Users\Amir Khasru\Desktop\matrix_add.c|5|warning: no previous declaration for 'add' [-Wmissing-declarations]|
C:\Users\Amir Khasru\Desktop\matrix_add.c||In function 'main':|
C:\Users\Amir Khasru\Desktop\matrix_add.c|45|error: passing argument 1 of 'add' from incompatible pointer type|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Amir Khasru
  • 74
  • 1
  • 3
  • 10
  • ok, so what's the question? – Sourav Ghosh Apr 06 '16 at 21:27
  • What's the problem with the declaration? – Amir Khasru Apr 06 '16 at 21:32
  • 1
    http://stackoverflow.com/questions/9446707/correct-way-of-passing-2-dimensional-array-into-a-function – Unimportant Apr 06 '16 at 21:41
  • 2
    `int **` and `int [][]` are incompatible data types. Instead of `int **`, you need to use `int (*)[]` with the appropriate value for the array dimension (in this case, it's 5). So `int (*a)[5]`. If you want to allow a variable dimension, then you can pass that as an argument that precedes the array argument. – Tom Karzes Apr 06 '16 at 21:44
  • What happened to `#include ` A fragmented [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) is one card short of a full house. – Weather Vane Apr 06 '16 at 21:56
  • @MichaelPetch lovely to see you on a C thread. Is the wind from the Northbridge today? – Weather Vane Apr 06 '16 at 22:03
  • @WeatherVane : lol, it is a slow day on the assembler related tags. Was bored of editing questions lol. – Michael Petch Apr 06 '16 at 22:04
  • @MichaelPetch so... I should have said "Southbridge" perhaps. Sorry I deleted the earlier comment. – Weather Vane Apr 06 '16 at 22:06

3 Answers3

3

When passed, a, b and c decay to int (*)[5], rather than int **.

So you should rewrite your function as:

void add(int a[][5], int b[][5], int c[][5], size_t row, size_t col)
{
    size_t i, j;
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            c[i][j] = a[i][j] * b[i][j];
        }
    }
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
1

The confusion is in the fact that int x[5] is not a pointer. It can be used as a pointer, but it is actually a block of 5 ints. And by this logic, int y[5][5] is also not a double pointer. This however cannot be used as a double pointer. Simply because there is no actual pointer pointing to each row. It is just a bunch (25) of integers, of which you( and the compiler) know how big each row is. So that you know that element [0,4] are the first row [5,9] the second, etc. This means that you have to tell the function how log the rows are. There are two ways to do this. One is to explicitly make add accept blocks of 5x5

void add(int a[5][5],int b[5][5],int c[5][5],int row,int col){
   ...
}

The other is to pass it as a single pointer (eg. int* a), and to pass the width as another argument.

David van rijn
  • 2,108
  • 9
  • 21
1

In your add function, int** is NOT int[][] Both are extremely different datatypes, one is an array of arrays, and the other is a pointer to a pointer. You should declare it as a int (*name)[size] or alternatively (this happens to be my favorite way), typecast it to a pointer and then use simple pointer arithmetic -

int add(int *a, int *b, int *c, int rows, int columns, int rowsize){
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            *(c + rowsize*i + j) = (*(a + rowsize*i + j))*(*(b + rowsize*i + j));
        }
    }
    return 0;
}
Shahe Ansar
  • 314
  • 2
  • 12