in Programming in C
586 views
0 votes
0 votes

i wanted to intialize a 2d array using pointers to pointers and i wrote the following code i know the below code is lot of hardcoding but as i am a beginner to pointers i want to do it this way,please help me the code is not giving the output as segmentation fault.Everything seems well to me though.please take time and check this.

 #include<stdio.h> 
 #include<stdlib.h> 
 int readinput1(int **a,int row,int column); 
 int readinput2(int **b,int row,int column); 
 int sumoftables(int **a,int **b,int **c,int row,int column); 
 int displayresult(int **c,int row,int column); 
 int main() 
 { 
     int **a,**b,**c,row,column; 
     printf("no.of rows of desired matrix:"); 
     scanf("%d",&row); printf("no.of columns of desired matrix:"); 
     scanf("%d",&column); 
     a=(int**)malloc(row*column*sizeof(int));//can i do the memory allocation like this 
     readinput1(a,row,column); 
     b=(int**)malloc(row*column*sizeof(int)); 
     readinput2(b,row,column); 
     c=(int**)malloc(row*column*sizeof(int)); 
     sumoftables(a,b,c,row,column); 
     printf("\n\n\n"); 
     displayresult(c,row,column); 
 } 
 int readinput1(int **a,int row,int column) 
 { 
     int p,q; 
     for(p=0;p<row;p++) 
     { 
         for(q=0;q<column;q++) 
         { 
             scanf("\n%x",(*(a+p)+q)); 
         } 
     } 
     return 0; 
 } 
 int readinput2[enter image description here][1]t2(int **b,int row,int column) 
 { 
     int p,q; 
     for(p=0;p<row;p++) 
     { 
         for(q=0;q<column;q++) 
         { 
             scanf("\n%x",(*(b+p)+q)); 
         } 
     } 
     return 0; 
 } 
 int sumoftables(int **a,int **b,int **c,int row,int column) 
 { 
     int p,q; for(p=0;p<row;p++) 
     { 
         for(q=0;q<column;q++) 
         { 
             *(*(c+p)+q)=*(*(a+p)+q)+*(*(b+p)+q); 
         } 
     } 
     return 0; 
 } 
 int displayresult(int **c,int row,int column) 
 { 
     int r,s; for(r=0;r<row;r++) 
     { 
         for(s=0;s<column;s++) 
         { 
             printf("\t%d",*(*(c+r)+s)); 
         } 
         printf("\n"); 
     } 
     return 0; 
 }
    
   https://gateoverflow.in/?qa=blob&qa_blobid=17604332813334679423
in Programming in C
586 views

1 comment

a=(int**)malloc(row*column*sizeof(int));>can i do the memory allocation like this

Check the return value and if it is not error, it means you can.

And here, it is "yes". Now, logically it is wrong. Here, you are allocating a linear array of memory for the 2D array, which is fine if you also access it that way. But you are accessing it using **a and not *a. What happens with that is, when you use

*(a+p)+q,

it goes like (a+ p*sizeofint*)

there is a mistake here only, because sizeof int and sizeof int* are often different on 64 bit machines. But even otherwise assuming 32 bit and both as 4 bytes, we get

*(a + p * 4)

Now, this value is uninitialized and we are using it as an address. Ofcourse removing the '*' would correct that problem. But the full correct way is to use a pointer to int as shown below, or allocate a double pointer first and then a single pointer for each row. 

 scanf("%d",&column);
        a=(int*)malloc(row*column*sizeof(int));//>can i do the memory allocation like this
        readinput1(a,row,column);
       /.....

    }
    int readinput1(int *a,int row,int column)
    {
        int p,q;
        for(p=0;p<row;p++)
        {
            for(q=0;q<column;q++)
            {
                scanf("\n%x",(a+p*column+q));
            }
        }
        return 0;
    }
0
0

Please log in or register to answer this question.