0

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 .

  • sizeof works on types not variables if you want to use its value you need to write the variable name without sizeof instead of `( sizeof(martricesSum) * rows)` just `( martricesSum * rows)` – Asaf Itach May 04 '21 at 14:37
  • You might want to have a look at [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin May 04 '21 at 14:40
  • 1
    The general rule of thumb for `malloc`ing is the `sizeof` the object "one level up". So for an `int***` ([also a bad idea](https://stackoverflow.com/questions/21488544/triple-pointers-in-c-is-it-a-matter-of-style)), you would do `int*** threeStar = malloc(sizeof(int**) * nmemb);`, for an `int**` you would do `int** twoStar = malloc(sizeof(int*) * nmemb);`, etc. For easier maintenance (and my preferred method), do `int* ptr = malloc(sizeof(*ptr) * nmemb);`. The dereference retrieves the object "one level up", so even if the type of `ptr` changes, no further modifications are necessary. – yano May 04 '21 at 14:44
  • @yano I tried, but their is no positive result . – Nagraju Kasarla May 04 '21 at 15:14
  • 1
    Kudos for using Turbo C in 2021 (!) – Wör Du Schnaffzig May 04 '21 at 15:21
  • @pqans Yeah a person asked me to convert my code to TURBO C – Nagraju Kasarla May 04 '21 at 15:23

0 Answers0