int main(void)
{
int r,c,i,j,k,s;
printf("Enter the numbers of columns of the matrices: "); //asking the user about the rows and columns
scanf("%d%d",&r,&c);
int *arr = (int *)malloc(r * c * sizeof(int)); //dynamically alloting the array 1
int *a = (int *)malloc(r * c * sizeof(int)); //dyanamically alloting the array 2
int *m = (int *)malloc(r*c*sizeof(int)); //dynamically alloting the resulting array
printf("Enter the elements for the first matrix: \n"); //entering the elements of array 1
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d", (*( arr + r ) + c));
}
printf("\n");
}
printf("Enter the elements for the second matrix: \n"); //entering the elements of array 2
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d", (*( a + r ) + c));
}
printf("\n");
}
for(i=0;i<r;i++) //multiplying the matrices
{
for(j=0;j<c;j++)
{
s=0; //alloting an element and it's value as zero
for(k=0;k<c;k++)
{
s += (*(arr+r)+k) * (*(a+k)+ c);
}
*(*(m+r)+c) = s; //save it in the resulting matrix
}
}
printf("The multiplied matrix is: \n"); //printing the multiplied matrix
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",(*( m + r) + c));
printf("\n");
}
free(arr);
free(a);
free(m);
return 0;

- 13,833
- 2
- 22
- 31

- 3
- 1
-
`*(*(m+r)+c)` It's pretty clear what the error is saying. `*(m+r)+c` is an `int`. So it can't be dereferenced. – kaylum Mar 28 '21 at 10:33
-
I declared it as an int pointer dynamically right? – Surya Tejaswi Mar 28 '21 at 10:53
-
`m` is an int pointer. `*(m+r)` dereferences the pointer and results in an int. So you can't dereference that int result as you are doing in `*(*(m+r)+c)`. Two stars means there needs to be two pointers. You only have a single pointer. – kaylum Mar 28 '21 at 10:58
-
Ah thank you so much for the eye opener,was trying a lot to get that – Surya Tejaswi Mar 28 '21 at 11:19
-
@SuryaTejaswi This question and its answers does a really good job of explaining how arrays use memory: [**Correctly allocating multi-dimensional arrays**](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – Andrew Henle Mar 28 '21 at 13:05
2 Answers
There are multiple problems in your code, I'm guessing from misunderstandings about how pointers and arrays works.
First of all you need to learn that for any pointer (or array) m
and index r
, the expression *(m + r)
is exactly equal to m[r]
. If we do this substitution in your expression *(*(m + r) + c)
then it would be *(m[r] + c)
which it seems you want to be m[r][c]
.
There are at least three problems with it:
The first is that
m[r]
is anint
, som[r] + c
is also anint
, and that can't be referenced.The second is that you use the array sizes in your expression, not the
i
andj
positions.The third problem is that while arrays of arrays can be emulated using one single-dimension array, they don't work like pointers to pointers, and the arithmetic to get an element in them doesn't work using plain addition only.
While the first two points should be pretty easy to understand, the last could be a little harder. First one need to know how an array of arrays is laid out in memory, so lets take a simple array:
int m[2][3];
This will look something like this in memory:
+---------+---------+---------+---------+---------+---------+ | m[0][0] | m[0][1] | m[0][2] | m[1][0] | m[1][1] | m[1][2] | +---------+---------+---------+---------+---------+---------+
To get to m[i][j]
we can't just add i
and j
together to get the offset. For example lets take the case of i == 0
and j == 1
, then i + j
would be 1
. This clashes with i == 1
and j == 0
where again the sum of i + j
would also be 1
. So both cases (i == 0 && j == 1
and i == 1 && j == 0
) would use the same element, which we don't want.
Instead we need to use multiplication for the "row" number (i
in this case), and addition for the column (j
). And we need to multiply with the row-size 2
. So to get m[i][j]
we would use m + (i * 2) + j
(using the example array defined above).
So in your code, whenever you attempt to use a single cell in the "matrix" you need to use m[i * r + j]
.

- 400,186
- 35
- 402
- 621
-
Can you please help me with assigning s to the product of the elements of the matrix,I seem to have a problem with it – Surya Tejaswi Mar 28 '21 at 11:35
-
-
Yes but I don't quite seem to find the alternative for s += (*(arr+r)+k) * (*(a+k)+ c); – Surya Tejaswi Mar 29 '21 at 02:30
-
@SuryaTejaswi First you have to decide which of the index variables `i`, `j` or `k` you need to use and where. – Some programmer dude Mar 29 '21 at 05:27
-
The pointer arr
(and other pointers in your program) declared like
int *arr = (int *)malloc(r * c * sizeof(int));
has the type int *
. So dereferencing the pointer
scanf("%d", (*( arr + r ) + c));
you get an object of the type int
while the function scanf
expects a pointer..
Moreover in these loops
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d", (*( arr + r ) + c));
}
printf("\n");
}
the expression (*( arr + r ) + c)
depend neither on i
nor on j
.
You should write at least like
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d", arr + i * c + j );
}
printf("\n");
}
Another approach is indeed to allocated a two-dimensional variable length array like
int ( *arr )[c] = (int *)malloc( sizeof( int[r][c] ) );
and to use two indices to access elements of the array.
Pay attention to that the matrices have incorrect dimensions for the multiplication operation. That is if the first matrix has dimensions r
and c
then the second matrix should have dimensions c
and r
and the result matrix will have dimensions r
and r
.

- 301,070
- 26
- 186
- 335