dmilicev

c_matrices_with_pointers_with_dynamic_memory_allocation_v2.c

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