Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- c_matrices_with_pointers_with_dynamic_memory_allocation.c
- Updated: 2021.05.05 , by Dragan Milicev, https://www.facebook.com/dmilicev
- https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
- 1) Using a single pointer:
- A simple way is to allocate memory block of size r*c
- and access elements using simple pointer arithmetic.
- 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 that has r rows and c columns.
- // title describes the shown matrix arr.
- 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*c + 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=0;
- for (i=0; i<r; i++)
- for (j=0; j<c; j++)
- *(arr + i*c + j) = ++count;
- } // fillMatrix()
- // Reallocate matrix space of the matrix arr[i][j] which has r rows and c columns.
- int *reallocateMatrixSpace( int *arr, int r, int c ){
- // allocate sufficient space
- arr = (int *)realloc( arr, r * c * sizeof(int) );
- if( arr == NULL ) {
- fprintf(stderr, "\n\n r=%d , c=%d , arr realloc() error! \n\n", r, c);
- exit(EXIT_FAILURE);
- }
- return arr;
- } // reallocateMatrixSpace()
- // Gets matrix element arr[i][j] of the matrix arr 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 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
- return( *(arr + i*c + j) );
- } // getMij()
- // Sets the matrix element arr[i][j] of the matrix arr 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*c + j) = newValue;
- return(arr);
- }
- } // setMij()
- int main(){
- int r=3, c=4; // rows and columns of matrix arr[r][c]
- char *title = (char*)malloc( 100 * sizeof(char) ); // string for titles
- int *arr; // matrix arr[r][c]
- // allocate sufficient space
- arr = (int *)malloc( r * c * sizeof(int) );
- if( arr == NULL ) {
- fprintf(stderr, "\n\n arr malloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- printf("\n -------------------------------------------------- \n");
- fillMatrix(arr,r,c);
- sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );
- printMatrix(title,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 different matrix
- r = 5;
- c = 6;
- reallocateMatrixSpace(arr,r,c);
- fillMatrix(arr,r,c);
- sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );
- printMatrix(title,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 different matrix
- r = 3;
- c = 3;
- reallocateMatrixSpace(arr,r,c);
- fillMatrix(arr,r,c);
- sprintf(title, "\n Matrix arr[%d][%d] is: \n\n", r, c );
- printMatrix(title,arr,r,c);
- printf("\n arr[%d][%d] = %d \n", r-1, c-1, getMij(arr,r,c,r-1,c-1) );
- setMij(arr,r,c,r-1,c-1,12);
- printMatrix(title,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(arr); // free reserved memory for arr[r][c] with function malloc()
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement