1
I would like to modify my matrix ( multiplying by 2 each of its elements) using pointers, but I don’t know why my code isn’t working...
#include<stdio.h>
void changematrix(int **mm,int row, int column)
{
int i,j;
for( i = 0;i < row; i++)
{
for( j = 0; j < column; j++)
{
*(*(mm + i) + j) = 2* *(*(mm + i) + j);
}
}
}
int main()
{
int i,j;
int row, column;
printf("Type the number of row\n");
scanf("%d", &row);
printf("Type the number of columns\n");
scanf("%d",&column);
int mat[row][column];
printf("Now type the numbers\n");
for( i = 0; i < row; i++)
{
for( j = 0; j < column; j++)
{
scanf("%d", &mat[i][j]);
}
}
changematrix(&mat,row,column);
for( i = 0; i < row; i++)
{
for( j = 0; j < column; j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
}
I had a doubt in the memory allocation part, inside the for wouldn’t be malloc ( column * sizeof(int)) ? if it is Row itself, pq would be this?
– Vitor Matos
Well observed, the for condition is also wrong, I ended up reversing the two. I already corrected the answer code.
– Lucas Trigueiro