0

I can't understand why this piece of code which is supposed to perform matrix multiplication goes wrong.

Input: 2x2 matrices with elements 1,2,3,4 in both matrices

Expected output: 7 10 15 22

Output given by this code: 15 22 12 16

int a[10][10], b[10][10], c[10][10], i, j, k, r1, c1, r2, c2;

int (*pa)[10][10] = &a, (*pb)[10][10] = &b, (*pc)[10][10] = &c;

for ( i = 0; i < r1; i++) {
    for(j = 0; j < c2; j++) {
         *pc[i][j] = 0;
        for(k = 0; k < c1; k++) {
            *pc[i][j] += *pa[i][k] * *pb[k][j];
        }
    }
}

I tried debugging using print statements like this and these are the results:

When given 2x2 matrices which have 1,2,3,4 as their elements, these are the errors produced:

at 00 of a is 3
at 00 of b is 1
Elements you're multiplying: 3  1

But expected output is this:

at 00 of a is 1
at 00 of b is 1

(Same seems to happen for rest of the elements) Rest of the code which isn't pasted here is bug-free. Checked it thoroughly using print statements.

Maverick
  • 1,519
  • 18
  • 32
  • 1
    check this answer - https://stackoverflow.com/questions/48245019/multiplication-of-2d-arrays-using-pointers-to-pointers?rq=1 – Prajilesh Feb 13 '19 at 05:09
  • 1
    `*pc[i][j]` should be `(*pc)[i][j]`, and the same for your other pointers. The indexing operator (`[]`) has higher precedence than the dereferencing operator (`*`), so you need parentheses to obtain the wanted order of operations. – John Bollinger Feb 13 '19 at 05:24
  • @John Bollinger I tried this approach and it didn't work. :(( Just came to know that pointer to int array[3] would point to array of three integers and pointer arithmetic(say, pointer + 1) would point to address beyond these three integers. If this is the case, there's no way to access elements of a 2D array using pointer to 2D array, right?? – Manogyana T Feb 13 '19 at 05:34
  • 1
    I can't speak to why a piece of code different from what you've presented also does not work, but I assure you that with your pointers `pa`, `pb`, and `pc` declared and initialized as you show, `a[i][j] == (*pa)[i][j]` for all `i` and `j` not exceeding the array bounds, and similarly for the other array pointers. – John Bollinger Feb 13 '19 at 05:39
  • Pointers *to* 2D arrays work exactly like pointers to 1D arrays or pointers to any other data. No difference whatsoever. We rarely use pointers *to* arrays though. Pointers *into* arrays normally do the job. In this code, pointers are not needed at all. Why not ditch them and just use a, b, and c directly? – n. m. could be an AI Feb 13 '19 at 06:26
  • @n.m. Yes. Sadly, this is an assignment question. :(( – Manogyana T Feb 15 '19 at 14:59
  • Are you sure you interpret your assignment correctly? People often say "pointer to an array" when they really mean "pointer into an array". Pointers *to* arrays are rather trivial. An assignment that requires you to use pointers *to* arrays would not teach you any valuable skills. One that requires you to use pointers *into* arrays will. Which is why I conjecture that your teacher wants you to do something entirely different from using `(*pc)[i][j]`. – n. m. could be an AI Feb 15 '19 at 15:25

1 Answers1

0

Refer these answers first:

Pointer address in a C multidimensional array

Create a pointer to two-dimensional array

Both together answer you question. Here is a working version of your code(simple version):

#include<stdio.h>

void main()
{
    int a[2][2]={{1,2},{3,4}};
    int b[2][2]={{1,2},{3,4}};
    int c[2][2], i, j, k, r1=2, c1=2, r2=2, c2=2;

    int (*pa)[2] = a, (*pb)[2] = b, (*pc)[2] = c;

    for ( i = 0; i < r1; i++) 
    {
        for(j = 0; j < c2; j++) 
        {
            pc[i][j] = 0;
            for(k = 0; k < c1; k++) 
            {
                pc[i][j] += pa[i][k] * pb[k][j];
            }
        }
    }
    for(i=0;i<2;i++)
    {
        printf("\n");
        for(j=0;j<2;j++)
        {
            printf("%d\t",c[i][j]);
        }
    }

}
karel
  • 5,489
  • 46
  • 45
  • 50
M. Knight
  • 89
  • 5