dmilicev

c_matrices_with_pointers_with_dynamic_memory_allocation.c

May 4th, 2021 (edited)
1,023
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. // Prints a matrix M that has r rows and c columns.
  20. // title describes the shown matrix M.
  21. void printMatrix( char *title, int *M, int r, int c ){
  22.     int i,j;
  23.     printf("%s", title );
  24.     for (i=0; i<r; i++){
  25.         for (j=0; j<c; j++)
  26.             printf("%4d", *(M + i*c + j) );
  27.         printf("\n\n");
  28.     }
  29. } // printMatrix()
  30.  
  31. // Fills in the matrix M[i][j] which has r rows and c columns with ordinal numbers.
  32. void fillMatrix( int *M, int r, int c ){
  33.     int i, j, count=0;
  34.     for (i=0; i<r; i++)
  35.         for (j=0; j<c; j++)
  36.             *(M + i*c + j) = ++count;
  37. } // printMatrix()
  38.  
  39. // Fills in the matrix M[i][j] which has r rows and c columns with ordinal numbers.
  40. int *reallocateMatrixSpace( int *M, int r, int c ){
  41.     // allocate sufficient space
  42.     M = (int *)realloc( M, r * c * sizeof(int) );
  43.     if( M == NULL ) {
  44.         fprintf(stderr, "\n\n r=%d , c=%d , M realloc() error! \n\n", r, c);
  45.         exit(EXIT_FAILURE);
  46.     }
  47.     return M;
  48. } // reallocateMatrixSpace()
  49.  
  50. // Returns M[i][j], an element of the matrix M that has r rows and c columns
  51. int getMatrixElement(int *M, int r, int c, int i, int j){
  52.     int error=0;
  53.     // check that i and j are valid
  54.     if( i<0 || i>r-1 ){
  55.         printf("\n Index i = %d    for rows r = %d of matrix M is not valid ! \n", i, r );
  56.         error=1;
  57.     }
  58.     if( j<0 || j>c-1 ){
  59.         printf("\n Index j = %d for columns c = %d of matrix M is not valid ! \n", j, c );
  60.         error=1;
  61.     }
  62.     if(error)
  63.         return 0;
  64.     else
  65.         return( *(M + i*c + j) );
  66. } // getMatrixElement()
  67.  
  68.  
  69. int main(){
  70.     int r=3, c=4;                       // rows and columns of matrix M[r][c]
  71.     char *title = (char*)malloc( 100 * sizeof(char) );  // string for titles
  72.     int *M;                                                 // matrix M[r][c]
  73.  
  74.     printf("\n -------------------------------------------------- \n");
  75.     // allocate sufficient space
  76.     M = (int *)malloc( r * c * sizeof(int) );
  77.     if( M == NULL ) {
  78.         fprintf(stderr, "\n\n M malloc() error! \n\n");
  79.         exit(EXIT_FAILURE);
  80.     }
  81.     fillMatrix(M,r,c);
  82.     sprintf(title, "\n Matrix M[%d][%d] is: \n\n", r, c );
  83.     printMatrix(title,M,r,c);
  84.     printf("\n M[%d][%d] = %d \n", r-1, c-1, getMatrixElement(M,r,c,r-1,c-1) );
  85.  
  86.     printf("\n -------------------------------------------------- \n");
  87.     // reallocate sufficient space for different matrix
  88.     r = 5;
  89.     c = 6;
  90.     reallocateMatrixSpace(M,r,c);
  91.     fillMatrix(M,r,c);
  92.     sprintf(title, "\n Matrix M[%d][%d] is: \n\n", r, c );
  93.     printMatrix(title,M,r,c);
  94.     printf("\n M[%d][%d] = %d \n", r-1, c-1, getMatrixElement(M,r,c,r-1,c-1) );
  95.  
  96.     printf("\n -------------------------------------------------- \n");
  97.     // reallocate sufficient space for different matrix
  98.     r = 3;
  99.     c = 3;
  100.     reallocateMatrixSpace(M,r,c);
  101.     fillMatrix(M,r,c);
  102.     sprintf(title, "\n Matrix M[%d][%d] is: \n\n", r, c );
  103.     printMatrix(title,M,r,c);
  104.     printf("\n M[%d][%d] = %d \n", r-1, c-1, getMatrixElement(M,r,c,r-1,c-1) );
  105.  
  106.     printf("\n -------------------------------------------------- \n");
  107.  
  108.     /* Code for further processing and free the
  109.       dynamically allocated memory */
  110.  
  111.     free(M);
  112.     return 0;
  113. }
  114.  
RAW Paste Data