Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- c_matrices_with_pointers_with_dynamic_memory_allocation_v4.c
- Updated: 2021.05.11 , by Dragan Milicev, https://www.facebook.com/dmilicev
- https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
- 4) Using double pointer and one malloc call
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h>
- // Prints a matrix arr[r][c] that has r rows and c columns.
- // title describes the shown matrix arr[r][c].
- void printMatrix( char *title, int **arr, int r, int c ){
- int i,j;
- printf("%s", title );
- for(i=0; i<r; i++){
- for(j=0; j<c; j++)
- printf("%4d", arr[i][j]); // Note that arr[i][j] is same as *(*(arr+i)+j)
- printf("\n\n");
- }
- } // printMatrix()
- // Fills in the matrix arr[i][j] which has r rows and c columns with ordinal numbers.
- void fillMatrix( int **arr, int r, int c ){
- int i, j, count=1;
- for(i=0; i<r; i++)
- for(j=0; j<c; j++)
- arr[i][j] = count++; // Note that arr[i][j] is same as *(*(arr+i)+j)
- } // fillMatrix()
- // Reallocate matrix space of the matrix arr[i][j].
- // The new matrix has r_new rows and a c_new columns.
- // The old matrix had r_old rows and a c_old columns.
- int **reallocateMatrixSpace( int **arr, int r_new, int c_new, int r_old, int c_old ){
- int i;
- for(i=0; i<r_new; i++){
- if( r_new < r_old ){ // reallocate sufficient space
- arr[i] = (int *)realloc( arr[i], c_new * sizeof(int) );
- if( arr[i] == NULL ) {
- fprintf(stderr, "\n\n arr[%d] realloc() error! \n\n", i);
- exit(EXIT_FAILURE);
- }
- }else{ // allocate sufficient space
- arr[i] = (int *)malloc( c_new * sizeof(int) );
- if( arr[i] == NULL ) {
- fprintf(stderr, "\n\n arr[%d] malloc() error! \n\n", i);
- exit(EXIT_FAILURE);
- }
- }
- }
- // if there are extra rows, we free the memory that was reserved for them
- for(i=r_new; i<r_old; i++)
- free( arr[i] );
- return arr;
- } // reallocateMatrixSpace()
- // Gets matrix element arr[i][j] of the matrix arr[r][c] that has r rows and c columns
- int getMij(int **arr, int r, int c, int i, int j){
- int error=0;
- // check that i and j are valid
- if( i<0 || i>r-1 ){
- printf("\n Index i = %d for rows r = %d of matrix M is not valid ! \n", i, r );
- error=1;
- }
- if( j<0 || j>c-1 ){
- printf("\n Index j = %d for columns c = %d of matrix M is not valid ! \n", j, c );
- error=1;
- }
- if(error)
- return 0;
- else
- return arr[i][j]; // or return *(*(arr+i)+j);
- } // getMij()
- // Sets the matrix element arr[i][j]
- // of the matrix arr[r][c] that has r rows and c columns to the value newValue
- int **setMij(int **arr, int r, int c, int i, int j, int newValue){
- int error=0;
- // check that i and j are valid
- if( i<0 || i>r-1 ){
- printf("\n Index i = %d for rows r = %d of matrix arr is not valid ! \n", i, r );
- error=1;
- }
- if( j<0 || j>c-1 ){
- printf("\n Index j = %d for columns c = %d of matrix arr is not valid ! \n", j, c );
- error=1;
- }
- if(error)
- return 0;
- else{
- arr[i][j] = newValue; // or *(*(arr+i)+j) = newValue;
- return(arr);
- }
- } // setMij()
- int main(){
- int i, len=0, r=3, c=4, r_old, c_old; // rows and columns of matrix arr[r][c]
- char *title = (char*)malloc( 100 * sizeof(char) ); // string for titles
- int *ptr, **arr; // pointer to pointer of matrix arr[r][c]
- len = sizeof(int *) * r + sizeof(int) * c * r;
- arr = (int **)malloc(len);
- if( arr == NULL ) {
- fprintf(stderr, "\n\n arr malloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- // ptr is now pointing to the first element in of 2D array
- ptr = (int *)(arr + r);
- // for loop to point rows pointer to appropriate location in 2D array
- for(i = 0; i < r; i++)
- arr[i] = (ptr + c * i);
- printf("\n -------------------------------------------------- \n");
- fillMatrix(arr,r,c); // fill matrix arr[i][j] with ordinal numbers
- sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c ); // make title
- printMatrix(title, arr, r, c); // print matrix arr[r][c]
- printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
- printf("\n -------------------------------------------------- \n");
- // reallocate sufficient space for new different matrix
- r_old = r; // we remember the old values
- c_old = c;
- r = 6; // new values
- c = 7;
- reallocateMatrixSpace( arr, r, c, r_old, c_old ); // reallocate memory space
- fillMatrix(arr,r,c); // fill matrix arr[i][j] with ordinal numbers
- sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c ); // make title
- printMatrix(title,arr,r,c); // print matrix arr[r][c]
- printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
- printf("\n -------------------------------------------------- \n");
- // reallocate sufficient space for new different matrix
- r_old = r; // we remember the old values
- c_old = c;
- r = 5; // new values
- c = 6;
- reallocateMatrixSpace( arr, r, c, r_old, c_old ); // reallocate memory space
- fillMatrix(arr,r,c); // fill matrix arr[i][j] with ordinal numbers
- sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c ); // make title
- setMij(arr, r, c, r-1, c-1, 111 );
- printMatrix(title,arr,r,c); // print matrix arr[r][c]
- printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
- printf("\n -------------------------------------------------- \n");
- // Code for further processing and free the dynamically allocated memory
- // free memory reserved by malloc() for matrix arr[r][c]
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement