Rakesh
Rakesh

Reputation: 1

i do not understand the memory allocation of element in 2-D array using function

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 *

Upvotes: 0

Views: 58

Answers (1)

Lundin
Lundin

Reputation: 215360

The only reason why you would ever use the evil-looking int*** is when you return a pointer-to-pointer through a parameter. Your function input is all wrong.

  • pointer= malloc(sizeof(int*)*row); is wrong, you should assign a pointer-to-pointer to where pointer points at: *pointer = malloc....
  • *(pointer + i) First of all, this style is pretty much always wrong since it is just a less readable version of pointer[i]. However, that would be wrong too, since we need to assign each pointer previously allocated to point at an array. That would be (*pointer)[i] = ...
  • Similarly, scanf("%d",&pointer[i][j]); should be &((*pointer)[i][j]).

We could have avoided all this mess if we had done int** tmp then done all malloc on that variable, and then at the very end of the function *pointer = tmp;.

The function calculate similarly does not make any sense. You don't need 3 levels of indirection at all there. Nor do you need a local array to hold the results.

More importantly: this whole way of allocating 2D arrays in intrinsically wrong to begin with! It might have made sense for arrays of strings, so called "jagged arrays". But it doesn't make any sense whatsoever for a 2D matrix. Detailed explanation regarding why your teacher/book is teaching you the wrong things here: Correctly allocating multi-dimensional arrays. Please share with the would-be teacher/author...

A simplified, somewhat beginner-friendly version of parts of your code might look like this:

void* get_2D_matrix (int row, int col)
{
    // allocate an actual 2D array:
    int (*matrix)[col] = malloc( sizeof(int[row][col]) );
    if(matrix == NULL)
    {
      /* error handling */
    }

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            scanf("%d",&matrix[i][j]); 
        }
    }
    return matrix;
}


int main ()
{
    int row=3, col=2; 

    int (*matrix1)[col] = get_2D_matrix(row, col) ;
    ...

        do_something(matrix1[i][j]);

    ...

    free(matrix); // just one call required.

Upvotes: 0

Related Questions