Here I'm using TURBO C compiler.
I don't understand where I did wrong in my code.
I used pointer-pointer-pointer (***int array ) for allocating memory for matrices.
int main(void)
{
int ***arrayMatrices,
rows,
columns,
matrices;
clrscr();
fputs ("\n Enter the No. of Matrices : ", stdout);
if( scanf ("%d",&matrices) != 1 )
{
puts ("\n\n You Made wrong choice !!");
return 1;
}
fputs ("\n Enter the No. of rows : ",stdout);
if ( scanf ("%d",&rows) != 1 )
{
puts ("\n\n You Made wrong choice !!");
return 1;
}
fputs ("\n Enter the No. of columns : ", stdout);
if ( scanf ("%d",&columns) != 1 )
{
puts ("\n\n You Made wrong choice !!");
return 1;
}
//allocating memory for arrayMatrices
arrayMatrices = ( int*** )malloc(matrices * sizeof(arrayMatrices));
//allocating memory for arrayMatrices[i]
for ( int i = 0; i < matrices; i++ )
{
arrayMatrices[i] = ( int** )malloc(sizeof(arrayMatrices[i]) * rows);
printf ("\n Enter elements of Matrix-%d ! \n ", i+1);
for ( int j = 0; j < rows; j++ )
{
//allocating memory for arrayMatrices[i][j]
arrayMatrices[i][j] = ( int* )malloc(sizeof(arrayMatrices[i][j]) * columns);
for ( int k = 0; k < columns ; k++ )
{
//storing values in allocated memories
printf ("\n Enter element[%d][%d] : ", j+1, k+1);
scanf ("%d", &arrayMatrices[i][j][k]);
}
}
}
}
This is how I'm adding the matrices and printing
//Allocating memory for martricesSum variable to add matrices
int **martricesSum = ( int** )malloc( sizeof(martricesSum) * rows);
//Allocating memory for martricesSum rows
for ( int x = 0; x < matrices; x++ )
{
martricesSum[x] = (int*)malloc(sizeof(martricesSum[x]) * columns);
}
//Addition of matrices
for ( int u = 0; u < matrices; u++ )
{
for ( int v = 0; v < rows; v++ )
{
for ( int w = 0; w < columns ; w++ )
{
martricesSum[v][w] += arrayMatrices[u][v][w];
}
}
}
//Printing added matrices
printf ("\n Addition of above matrices !\n");
for ( int q = 0; q < rows; q++ )
{
for ( int r = 0; r < columns; r++ )
{
printf ("%3d", martricesSum[q][r]);
/*Here something is going wrong
Its printing only addresses instead of original values
I tried to place a * dereferencing operator (*) before
martricesSum[q][r]) but its not working */
}
putchar ('\n');
}
when I compile the code it printing addresses instead of values which are stored in memory .