dmilicev

c_matrices_with_pointers_with_dynamic_memory_allocation_v3.c

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