3

I am trying to add two matrices using 2D arrays in C. Here I am passing the array name as a parameter, which I think is pass by reference. However, I cannot add the arrays. Something is fishy in the add function, I cannot add the arrays.

I even tried to pass the parameter using address of Operator, however arrays are pointer themselves so it got error. I tried passing with the name of array. I am stuck

#include<stdio.h>
void input(int*,int*);
void add(int*,int*,int*);
void display(int*);

int main()
{
int a[3][3],b[3][3],sum[3][3];
 input(a,b);
 add(a,b,sum);
 display(sum);
 return 0;
 }

void input(int*a,int*b)
{
    for(int i = 0;i<3;i++)
    {
        for(int j =0;j<3;j++)
        {
            printf("Enter the element at a[%d][%d]",i+1,j+1);
            scanf("%d",((a+i)+j));
        }
    }
    for(int i = 0;i<3;i++)
    {
        for(int j =0;j<3;j++)
        {
            printf("Enter the element at b[%d][%d]",i+1,j+1);
            scanf("%d",((b+i)+j));
        }
    }
}
    void add(int* a,int*b,int*sum)
    {
        for(int i =0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                *(sum[i]+j) = *(a[i]+j) + *(b[i]+j);//error at this line
            }
        }
    }
 
 void display(int* sum)
  {
      printf("The sum is:\n");
      for(int i =0;i<3;i++)
      {
          for(int j =0;j<3;j++)
          {
            printf("%d ",(sum[i]+j));
          }
           printf("\n");
      }
  }

I got the following error

operand of '*' must be a pointer

However my operand is readily a pointer.

1
  • 2
    Mine gives the following error "note: expected 'int ' but argument is of type 'int ()[3]'" Commented Dec 13, 2020 at 17:02

2 Answers 2

4

The problem is that the methods:

void input(int*, int*);
void add(int*, int*, int*);
void display(int*);

expected a pointer, however you are passing to them 2D arrays (statically allocated), namely int a[3][3], b[3][3], sum[3][3];. Therefore, as already pointed out in the comments, those 2D arrays will be converted to 'int (*)[3]'. Consequently, you need to adapt the signature of your methods to:

void input(int [][3], int [][3]);
void add(int [][3], int [][3], int [][3]);
void display(int [][3]);

and

void input(int a[][3], int b[][3]){
      // the code
}

void add(int a [][3], int b [][3], int sum [][3]){
      // the code
}

void display(int sum [][3]){
      // the code
}    

Alternatively, if you want to keep the int* as parameter then what you can do is to allocate the matrix as a single array and adapt your code accordingly

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

Comments

1

Here I am passing the array name as a parameter,which i think is pass by refernece.However I cannot add the arrays.Something is fishy in the add function,i cannot add the arrays

You can use int* or use int[][], but you need to write the program as required by each case. I will show you an example of both cases.

about your program

  • do not use void. It is in general a waste, sometimes an error. Return something.
  • write generically. Compare:
 void display(int* sum)
  {
      printf("The sum is:\n");
      for(int i =0;i<3;i++)
      {
          for(int j =0;j<3;j++)
          {
            printf("%d ",(sum[i]+j));
          }
           printf("\n");
      }

  }

with

void display_a(int s[][3], int l, const char* msg)
{
    if( *msg != 0) printf("%s\n",msg);
    for(int i =0;i<l;i++)
    {
        for(int j =0;j<3;j++)
        {
        printf("%d ", s[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    return;
};

In the second case you can use the same function to display ANY array s and also write a simple title. Note that in C you can omit all but the last dimension in the vector. In fact C does not have multidimensional array as e.g. FORTRAN has. And passing the number of lines and the message as parameters you have much more than just using as you wrote.

  • NEVER write

    printf("Enter the element at a[%d][%d]",i+1,j+1);
     scanf("%d",((a+i)+j));
    

always test the return of scanf(). scanf() is not meant to read from the keyboard. Use at least one space in every prompt. Is is far more confortable to the user.

  • Use constants to test your program. Avoid reading for the user as it will waste much time on testing.

An example

output

Using arrays

Matriz A at [1][1] 1
Matriz A at [1][2] 2
Matriz A at [1][3] 3
Matriz A at [2][1] 4
Matriz A at [2][2] 5
Matriz A at [2][3] 6
Matriz B at [1][1] 6
Matriz B at [1][2] 5
Matriz B at [1][3] 4
Matriz B at [2][1] 3
Matriz B at [2][2] 2
Matriz B at [2][3] 1
The 1st matrix:
1 2 3
4 5 6

The 2nd matrix:
6 5 4
3 2 1

The sum (using arrays) is:
7 7 7
7 7 7

Using pointers

Matriz A at [1][1] 2
Matriz A at [1][2] 3
Matriz A at [1][3] 4
Matriz A at [2][1] 5
Matriz A at [2][2] 6
Matriz A at [2][3] 7
Matriz B at [1][1] 7
Matriz B at [1][2] 6
Matriz B at [1][3] 5
Matriz B at [2][1] 4
Matriz B at [2][2] 3
Matriz B at [2][3] 2
The 1st matrix:
2 3 5
5 6 7

The 2nd matrix:
7 6 4
4 3 2

The sum (using pointers) is:
9 9 9
9 9 9


Using t1 t2

The t1 matrix:
1 2 3
4 5 6
7 8 9

The t2 matrix:
9 8 7
6 5 4
3 2 1

t1 + t2 (using pointers) is:
10 10 10
10 10 10
10 10 10

the code

this code uses all that I tried to explain, I believe. Pay attention to the way you write the code when using the pointers: you just use the address and displacement.No brackets are needed.

#include<stdio.h>

void    add_a(int[][3],int[][3],int[][3],int);
void    display_a(int[][3],int,const char*);
int     input_a(int[][3], int, const char*);

void    add_p(int*,int*,int*,int);
void    display_p(int*,int,const char*);
int     input_p(int*, int, const char*);

int main(void)
{
    int t1[][3] =
    {
        { 1,2,3 },
        { 4,5,6 },
        { 7,8,9 }
    };

    int t2[][3] =
    {
        { 9,8,7 },
        { 6,5,4 },
        { 3,2,1 }
    };
    
    int a[3][3];
    int b[3][3];
    int sum[3][3];

    printf("Using arrays\n\n");

    input_a(a,2,"Matriz A");
    input_a(b,2,"Matriz B");
    display_a(a, 2, "The 1st matrix:");
    display_a(b, 2, "The 2nd matrix:");

    add_a( a,b,sum, 2 );
    display_a(sum, 2, "The sum (using arrays) is:");

    printf("Using pointers\n\n");

    input_p((int*) a,2,"Matriz A");
    input_p((int*) b,2,"Matriz B");
    display_p( (int*) a, 2, "The 1st matrix:");
    display_p( (int*) b, 2, "The 2nd matrix:");

    add_p(  (int*) a, (int*) b, (int*) sum, 2 );
    display_p( (int*) sum, 2, "The sum (using pointers) is:");


    printf("\nUsing t1 t2 \n\n");

    display_p( (int*) t1, 3, "The t1 matrix:");
    display_p( (int*) t2, 3, "The t2 matrix:");

    add_p(  (int*) t1, (int*) t2, (int*) sum, 3 );
    display_p( (int*) sum, 3, "t1 + t2 (using pointers) is:");

    return 0;
};

void add_a(
    int a[][3],
    int b[][3],
    int sum[][3],
    int l // lines
    )
{
    for(int i =0;i<l;i++)
        for(int j=0;j<3;j++)
            sum[i][j] = a[i][j] + b[i][j];
    return;
};
 
void display_a(int s[][3], int l, const char* msg)
{
    if( *msg != 0) printf("%s\n",msg);
    for(int i =0;i<l;i++)
    {
        for(int j =0;j<3;j++)
        {
        printf("%d ", s[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    return;
};

 int input_a(int a[][3], int l, const char* msg )
{
    for(int i = 0;i<l;i++)
    {
        for(int j =0;j<3;j++)
        {
            printf("%s at [%d][%d] ", msg, i+1,j+1);
            int res = scanf("%d",&a[i][j]);
            if ( res != 1 ) return -1; // read nothing
        }
    };
    return 0;
};

void add_p( int*a, int* b, int* sum, int l  )
{
    for(int i =0;i<l;i++)
        for(int j=0;j<3;j++)
            *(sum + i*l + j) =  *(a + i*l + j) + *(b + i*l + j);
    return;
};
 
void display_p(int* s, int l, const char* msg)
{
    if( *msg != 0) printf("%s\n",msg);
    for(int i =0;i<l;i++)
    {
        for(int j =0;j<3;j++)
            printf("%d ", *( s + i*l + j) );
        printf("\n");
    };
    printf("\n");
    return;
};

 int input_p(int* a, int l, const char* msg )
{
    for(int i = 0;i<l;i++)
    {
        for(int j =0;j<3;j++)
        {
            printf("%s at [%d][%d] ", msg, i+1,j+1);
            int res = scanf("%d", (a + i*l + j) );
            if ( res != 1 ) return -1; // read nothing
        }
    };
    return 0;
};

Comments

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.