Advertisement
dmilicev

c_matrices_with_pointers_with_dynamic_memory_allocation.c

May 4th, 2021 (edited)
1,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. /*
  2.  
  3.     c_matrices_with_pointers_with_dynamic_memory_allocation.c
  4.  
  5.     Updated: 2021.05.05  ,  by Dragan Milicev, https://www.facebook.com/dmilicev
  6.  
  7. https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
  8.  
  9. 1) Using a single pointer:
  10. A simple way is to allocate memory block of size r*c
  11. and access elements using simple pointer arithmetic.
  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 that has r rows and c columns.
  24. // title describes the shown matrix arr.
  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*c + 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=0;
  38.     for (i=0; i<r; i++)
  39.         for (j=0; j<c; j++)
  40.             *(arr + i*c + j) = ++count;
  41. } // fillMatrix()
  42.  
  43. // Reallocate matrix space of the matrix arr[i][j] which has r rows and c columns.
  44. int *reallocateMatrixSpace( int *arr, int r, int c ){
  45.     // allocate sufficient space
  46.     arr = (int *)realloc( arr, r * c * sizeof(int) );
  47.     if( arr == NULL ) {
  48.         fprintf(stderr, "\n\n r=%d , c=%d , arr realloc() error! \n\n", r, c);
  49.         exit(EXIT_FAILURE);
  50.     }
  51.     return arr;
  52. } // reallocateMatrixSpace()
  53.  
  54. // Gets matrix element arr[i][j] of the matrix arr that has r rows and c columns
  55. int getMij(int *arr, int r, int c, int i, int j){
  56.     int error=0;
  57.     // check that i and j are valid
  58.     if( i<0 || i>r-1 ){
  59.         printf("\n Index i = %d    for rows r = %d of matrix arr is not valid ! \n", i, r );
  60.         error=1;
  61.     }
  62.     if( j<0 || j>c-1 ){
  63.         printf("\n Index j = %d for columns c = %d of matrix arr is not valid ! \n", j, c );
  64.         error=1;
  65.     }
  66.     if(error)
  67.         return 0;
  68.     else
  69.         return( *(arr + i*c + j) );
  70. } // getMij()
  71.  
  72. // Sets the matrix element arr[i][j] of the matrix arr that has r rows and c columns
  73. // to the value newValue
  74. int *setMij(int *arr, int r, int c, int i, int j, int newValue){
  75.     int error=0;
  76.     // check that i and j are valid
  77.     if( i<0 || i>r-1 ){
  78.         printf("\n Index i = %d    for rows r = %d of matrix arr is not valid ! \n", i, r );
  79.         error=1;
  80.     }
  81.     if( j<0 || j>c-1 ){
  82.         printf("\n Index j = %d for columns c = %d of matrix arr is not valid ! \n", j, c );
  83.         error=1;
  84.     }
  85.     if(error)
  86.         return 0;
  87.     else{
  88.         *(arr + i*c + j) = newValue;
  89.         return(arr);
  90.     }
  91. } // setMij()
  92.  
  93.  
  94. int main(){
  95.     int r=3, c=4;                       // rows and columns of matrix arr[r][c]
  96.     char *title = (char*)malloc( 100 * sizeof(char) );  // string for titles
  97.     int *arr;                                           // matrix arr[r][c]
  98.  
  99.     // allocate sufficient space
  100.     arr = (int *)malloc( r * c * sizeof(int) );
  101.     if( arr == NULL ) {
  102.         fprintf(stderr, "\n\n arr malloc() error! \n\n");
  103.         exit(EXIT_FAILURE);
  104.     }
  105.  
  106.     printf("\n -------------------------------------------------- \n");
  107.  
  108.     fillMatrix(arr,r,c);
  109.     sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );
  110.     printMatrix(title,arr,r,c);
  111.     printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
  112.  
  113.     printf("\n -------------------------------------------------- \n");
  114.  
  115.     // reallocate sufficient space for different matrix
  116.     r = 5;
  117.     c = 6;
  118.     reallocateMatrixSpace(arr,r,c);
  119.     fillMatrix(arr,r,c);
  120.     sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );
  121.     printMatrix(title,arr,r,c);
  122.     printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
  123.  
  124.     printf("\n -------------------------------------------------- \n");
  125.  
  126.     // reallocate sufficient space for different matrix
  127.     r = 3;
  128.     c = 3;
  129.     reallocateMatrixSpace(arr,r,c);
  130.     fillMatrix(arr,r,c);
  131.     sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );
  132.     printMatrix(title,arr,r,c);
  133.     printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
  134.  
  135.     setMij(arr,r,c,r-1,c-1,12);
  136.     printMatrix(title,arr,r,c);
  137.     printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
  138.  
  139.     printf("\n -------------------------------------------------- \n");
  140.  
  141.  
  142.     /* Code for further processing and free the
  143.       dynamically allocated memory */
  144.  
  145.  
  146.     free(arr);  // free reserved memory for arr[r][c] with function malloc()
  147.  
  148.     return 0;
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement