first we declare pointer to pointer variable **matrix=NULL in main program .then we pass &matrix as argument in the function .in function it has parameter ex: void allocate(int **pointer , int row ,int column)now , i start to get confuse with pointer .as function has 3 astrik() so do i have to use (**pointer) or just (pointer) while allocating memory for the 2-D array .like below **pointer= malloc(sizeof(int *)*row); pointer =malloc(sizeof(int *)*rwo): after this i have another issue :
somehow i get to understand that pointer= malloc(sizeof(int *)row) is memory allocation for each row of the array but when it comes to allocation for each element i get confused more.because we declare (martrix)/(pointer in function ) as pointer to pointer right but when we do memory allocation for each element i see (pointer[i])/(poinetr +i).
for(int i = 0 ;i<row ; i++)
{
*(pointer +i)=malloc(sizeof(int)*column);
}
as my understanding when any pointer is done increment (++) with 1 it increase the size and ultimetly forms a array right. while here pointer being itself array of array (rows) then how.
*(pointer +i) is used to allocate the memory of the element in row . instead of forming a. array .
i am in complete confusion with pointer so please consider my question of even though make sense thank you here is my code:
Question: Function to get input matrix combine with dynamic memory allocation 1 Function to calculate calculate matrix1 – matrix2 and store them to the third matrix and print out the result matrix . 1 Function to free the matrix of any size (You have to create 3 function then using function call in the main to create the program and before read input matrix ask user to enter dimension for 2 input matrix)
#include<stdio.h>
#include<stdlib.h>
void input( int ***pointer , int row , int column )
{
pointer= malloc(sizeof(int*)*row);
for(int i = 0 ;i<row ; i++)
{
*(pointer + i)=malloc(sizeof(int )*column);
}
for(int i = 0 ;i<row ;i++)
{
for(int j = 0;j <column; j++)
{
scanf("%d",&pointer[i][j]);
}
}
}
void calculate(int ***pointer1 ,int ***pointer2 , int row , int column )
{
int **matrix3= malloc(sizeof(int *)*row);
for(int i =0 ;i<row ; i++)
{
*(matrix3 +i)=malloc(sizeof(int)*column);
}
for(int i= 0 ;i<row ;i++)
{
for(int j= 0;j <column; j++)
{
matrix3[i][j]=pointer1[i][j]-pointer2[i][j];
}
}
for(int i = 0 ; i<row ;i++)
{
for(int j = 0 ;j<column; j++)
{
printf("%d ",matrix3[i][j]);
}
printf("\n");
}
for(int i = 0;i<row ;i++)
{
free(*(matrix3 +i));
}
free(matrix3);
}
void frem(int ***pointer1,int row )
{
for(int i =0 ;i<row ;i++)
{
free(*(pointer1 +i));
}
free(pointer1);
}
int main ()
{
int row , column ;
scanf("%d%d",&row , &column);
int **matrix1= NULL ,**matrix2 =NULL ;
input(&matrix1,row , column );
input(&matrix2, row , column);
calculate(&matrix1 , &matrix2 , row , column);
frem(&matrix1, row );
frem(&matrix2, row );
return 0 ;
}
while i run the code it says
format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
16 | scanf("%d",&pointer[i][j]);
| ~^ ~~~~~~~~~~~~~~
| | |
| | int **
| int *