Advertisement
dmilicev

c_matrices_with_pointers_with_dynamic_memory_allocation_v4.c

May 11th, 2021
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.50 KB | None | 0 0
  1. /*
  2.     c_matrices_with_pointers_with_dynamic_memory_allocation_v4.c
  3.  
  4.     Updated: 2021.05.11  ,  by Dragan Milicev, https://www.facebook.com/dmilicev
  5.  
  6. https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
  7.  
  8. 4) Using double pointer and one malloc call
  9.  
  10.  
  11.     You can find all my C programs at Dragan Milicev's pastebin:
  12.  
  13.     https://pastebin.com/u/dmilicev
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. // Prints a matrix arr[r][c] that has r rows and c columns.
  21. // title describes the shown matrix arr[r][c].
  22. void printMatrix( char *title, int **arr, int r, int c ){
  23.     int i,j;
  24.     printf("%s", title );
  25.     for(i=0; i<r; i++){
  26.         for(j=0; j<c; j++)
  27.             printf("%4d", arr[i][j]);   // Note that arr[i][j] is same as *(*(arr+i)+j)
  28.         printf("\n\n");
  29.     }
  30. } // printMatrix()
  31.  
  32. // Fills in the matrix arr[i][j] which has r rows and c columns with ordinal numbers.
  33. void fillMatrix( int **arr, int r, int c ){
  34.     int i, j, count=1;
  35.     for(i=0; i<r; i++)
  36.         for(j=0; j<c; j++)
  37.             arr[i][j] = count++;    // Note that arr[i][j] is same as *(*(arr+i)+j)
  38. } // fillMatrix()
  39.  
  40. // Reallocate matrix space of the matrix arr[i][j].
  41. // The new matrix has r_new rows and a c_new columns.
  42. // The old matrix had r_old rows and a c_old columns.
  43. int **reallocateMatrixSpace( int **arr, int r_new, int c_new, int r_old, int c_old ){
  44.     int i;
  45.     for(i=0; i<r_new; i++){
  46.         if( r_new < r_old ){            // reallocate sufficient space
  47.             arr[i] = (int *)realloc( arr[i], c_new * sizeof(int) );
  48.             if( arr[i] == NULL ) {
  49.                 fprintf(stderr, "\n\n arr[%d] realloc() error! \n\n", i);
  50.                 exit(EXIT_FAILURE);
  51.             }
  52.         }else{                          // allocate sufficient space
  53.             arr[i] = (int *)malloc( c_new * sizeof(int) );
  54.             if( arr[i] == NULL ) {
  55.                 fprintf(stderr, "\n\n arr[%d] malloc() error! \n\n", i);
  56.                 exit(EXIT_FAILURE);
  57.                 }
  58.         }
  59.     }
  60.     // if there are extra rows, we free the memory that was reserved for them
  61.     for(i=r_new; i<r_old; i++)
  62.         free( arr[i] );
  63.     return arr;
  64. } // reallocateMatrixSpace()
  65.  
  66. // Gets matrix element arr[i][j] of the matrix arr[r][c] that has r rows and c columns
  67. int getMij(int **arr, int r, int c, int i, int j){
  68.     int error=0;
  69.     // check that i and j are valid
  70.     if( i<0 || i>r-1 ){
  71.         printf("\n Index i = %d    for rows r = %d of matrix M is not valid ! \n", i, r );
  72.         error=1;
  73.     }
  74.     if( j<0 || j>c-1 ){
  75.         printf("\n Index j = %d for columns c = %d of matrix M is not valid ! \n", j, c );
  76.         error=1;
  77.     }
  78.     if(error)
  79.         return 0;
  80.     else
  81.         return arr[i][j];       // or  return *(*(arr+i)+j);
  82. } // getMij()
  83.  
  84. // Sets the matrix element arr[i][j]
  85. // of the matrix arr[r][c] that has r rows and c columns to the value newValue
  86. int **setMij(int **arr, int r, int c, int i, int j, int newValue){
  87.     int error=0;
  88.     // check that i and j are valid
  89.     if( i<0 || i>r-1 ){
  90.         printf("\n Index i = %d    for rows r = %d of matrix arr is not valid ! \n", i, r );
  91.         error=1;
  92.     }
  93.     if( j<0 || j>c-1 ){
  94.         printf("\n Index j = %d for columns c = %d of matrix arr is not valid ! \n", j, c );
  95.         error=1;
  96.     }
  97.     if(error)
  98.         return 0;
  99.     else{
  100.         arr[i][j] = newValue;   // or  *(*(arr+i)+j) = newValue;
  101.         return(arr);
  102.     }
  103. } // setMij()
  104.  
  105.  
  106. int main(){
  107.     int i, len=0, r=3, c=4, r_old, c_old;   // rows and columns of matrix arr[r][c]
  108.     char *title = (char*)malloc( 100 * sizeof(char) );  // string for titles
  109.     int *ptr, **arr;                        // pointer to pointer of matrix arr[r][c]
  110.  
  111.     len = sizeof(int *) * r + sizeof(int) * c * r;
  112.  
  113.     arr = (int **)malloc(len);
  114.     if( arr == NULL ) {
  115.         fprintf(stderr, "\n\n arr malloc() error! \n\n");
  116.         exit(EXIT_FAILURE);
  117.     }
  118.  
  119.     // ptr is now pointing to the first element in of 2D array
  120.     ptr = (int *)(arr + r);
  121.  
  122.     // for loop to point rows pointer to appropriate location in 2D array
  123.     for(i = 0; i < r; i++)
  124.         arr[i] = (ptr + c * i);
  125.  
  126.     printf("\n -------------------------------------------------- \n");
  127.  
  128.     fillMatrix(arr,r,c);    // fill matrix arr[i][j] with ordinal numbers
  129.     sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );    // make title
  130.     printMatrix(title, arr, r, c);                  // print matrix arr[r][c]
  131.     printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
  132.  
  133.     printf("\n -------------------------------------------------- \n");
  134.  
  135.     // reallocate sufficient space for new different matrix
  136.     r_old = r;  // we remember the old values
  137.     c_old = c;
  138.     r = 6;      // new values
  139.     c = 7;
  140.     reallocateMatrixSpace( arr, r, c, r_old, c_old );   // reallocate memory space
  141.     fillMatrix(arr,r,c);        // fill matrix arr[i][j] with ordinal numbers
  142.     sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );    // make title
  143.     printMatrix(title,arr,r,c);                     // print matrix arr[r][c]
  144.     printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
  145.  
  146.     printf("\n -------------------------------------------------- \n");
  147.  
  148.     // reallocate sufficient space for new different matrix
  149.     r_old = r;  // we remember the old values
  150.     c_old = c;
  151.     r = 5;      // new values
  152.     c = 6;
  153.     reallocateMatrixSpace( arr, r, c, r_old, c_old );   // reallocate memory space
  154.     fillMatrix(arr,r,c);        // fill matrix arr[i][j] with ordinal numbers
  155.     sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );    // make title
  156.     setMij(arr, r, c, r-1, c-1, 111 );
  157.     printMatrix(title,arr,r,c);                     // print matrix arr[r][c]
  158.     printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
  159.  
  160.     printf("\n -------------------------------------------------- \n");
  161.  
  162.  
  163.     // Code for further processing and free the dynamically allocated memory
  164.  
  165.  
  166.     // free memory reserved by malloc() for matrix arr[r][c]
  167.     free(arr);
  168.  
  169.     return 0;
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement