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)) ===|
8
  • ok, so what's the question? Commented Apr 6, 2016 at 21:27
  • What's the problem with the declaration? Commented Apr 6, 2016 at 21:32
  • 1
    stackoverflow.com/questions/9446707/… Commented Apr 6, 2016 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. Commented Apr 6, 2016 at 21:44
  • What happened to #include <stdio.h> A fragmented Minimal, Complete, and Verifiable example is one card short of a full house. Commented Apr 6, 2016 at 21:56

3 Answers 3

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];
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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;
}

4 Comments

It is a nice way, but personally i think the notation c[ rowsize*i + j] more clear, but both is good.
@Davidvanrijn It's a personal preference, since it makes it easy for me to imagine it as a table or cuboid in my head when written like that.
Yea, whatever works for you :) I just wanted both ways on record. ( I wasn't critisizing)
@Davidvanrijn I know, I just wanted to explain

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.