By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this?
Second Program:
Expand|Select|Wrap|Line Numbers
- /* Best Method to allocate memory for 2D Array because it's
- much more flexible
- */
- /* int ***matrixptr -> **rowptr -> *rowptr[nrow] -> element of row
- 1st Matrix -> *rowptr[nrow] -> element of row
- -> *rowptr[nrow] -> element of row
- -> **rowptr -> *rowptr[nrow] -> element of row
- 2nd Matrix -> *rowptr[nrow] -> element of row
- -> *rowptr[nrow] -> element of row
- -> **rowptr -> *rowptr[nrow] -> element of row
- 3nd Matrix -> *rowptr[nrow] -> element of row
- -> *rowptr[nrow] -> element of row
- */
- // How to code this in C
- #include<stdio.h>
- #include<stdlib.h>
- #include<assert.h>
- /* int **rowptr -- A pointer to pointer to integer
- This can be explain through graphically.
- int **rowptr -> *rowptr[nrow] -> element of row
- -> *rowptr[nrow] -> element of row
- -> *rowptr[nrow] -> element of row
- */
- static void Allocate(int, int **, int **, int *, int *, int *, int *);
- //int Symmetric(int *, int *, int *, int *);
- void Add(int **, int **, int *, int *);
- void Subtract(int **, int **, int *, int *);
- void Display(int **, int *, int *);
- // int *nrow_ptr = NULL, *ncol_ptr = NULL;
- /* int *nrow_ptr, *ncol_ptr;
- // Declare the nrow_ptr and ncol pointer
- nrow_ptr = &nrow;
- ncol_ptr = &ncol;
- // Initialized the nrow_ptr and ncol_ptr
- */
- int main(int argc, char *argv[])
- {
- static int **rowptr;
- static int **rowptr_1;
- static int **result;
- int nMatrix;
- int nrow, ncol, nrow_1, ncol_1;
- // Number of row and column
- int *nrowptr;
- int *ncolptr;
- int *nrow_1ptr;
- int *ncol_1ptr;
- // Initialized the pointer pointing to nrow, ncol,
- // nrow_1, ncol_1 respectively
- nrowptr = &nrow;
- ncolptr = &ncol;
- nrow_1ptr = &nrow_1;
- ncol_1ptr = &ncol_1;
- /*
- Used while loop instead of dowhile because it's recommended
- by Bjarne Stroustrup
- */
- // ------------------------------------------------------------
- while(1)
- { // Must allocate at least two matrix
- printf("\nPlease Enter same dimension for two Matrix\n");
- printf("\nHow many Matrix : ");
- scanf("%d", &nMatrix);
- printf("\nHow many Row for First Matrix : ");
- scanf("%d", &nrow);
- printf("\nHow many Column for First Matrix : ");
- scanf("%d", &ncol);
- printf("\nHow many Row for Second Matrix : ");
- scanf("%d", &nrow_1);
- printf("\nHow many Column for Second Matrix : ");
- scanf("%d", &ncol_1);
- // ------------------------------------------------------------
- Allocate(nMatrix, rowptr, rowptr_1, nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
- if (rowptr || rowptr_1 == NULL)
- {
- perror("DMA for rowptr and rowptr_1 became NULL when came back from allcoate function");
- }
- Add(rowptr, rowptr_1, nrowptr, ncolptr);
- // Adding the element of matrix
- Subtract(rowptr, rowptr_1, nrowptr, ncolptr);
- // Subtract the element of matrix
- // Display(result, nrow, ncol);
- // Display the result after successful allocated the memory
- free(rowptr);
- free(rowptr_1);
- free(result);
- }
- return 0;
- }
- // -----------------------------------------------------------
- static void Allocate(int nMatrix, int **rowptr, int **rowptr_1,
- int *nrowptr, int *ncolptr,
- int *nrow_1ptr, int *ncol_1ptr)
- {
- // int symmetric;
- int row_loop, col_loop;
- switch(nMatrix)
- {
- case 1:
- {
- rowptr = (int **)malloc(sizeof(int **) * (*nrowptr));
- if (rowptr == NULL)
- {
- perror("Dynamic Memory Allocation for row Fails");
- }
- for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
- {
- rowptr[row_loop] = (int *)malloc(sizeof(int *) * (*ncolptr));
- if (rowptr[row_loop] == NULL)
- {
- perror("Dynamic Memory Allocation for column Fails");
- }
- }
- for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
- {
- for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
- {
- printf("Enter the [%d][%d] number : ", row_loop, col_loop);
- scanf("%d", &rowptr[row_loop][col_loop]);
- }
- }
- break;
- }
- // ------------------------------------------------------------
- case 2:
- { // Enter the first matrix
- // do
- // {
- printf("\nEnter the First Matrix : \n");
- // Allocate memory for rowptr **
- // if nrow == 3, then same as rowptr *[3]
- rowptr = (int **)malloc(sizeof(int **) * (*nrowptr));
- if (rowptr == NULL)
- {
- perror("Dynamic Memory Allocation for row Fails");
- }
- // For each rowptr * , allocate ncolumn
- for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
- {
- rowptr[row_loop] = (int *)malloc(sizeof(int *) * (*ncolptr));
- if (rowptr[row_loop] == NULL)
- {
- perror("Dynamic Memory Allocation for column Fails");
- }
- }
- for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
- {
- for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
- {
- printf("Enter the [%d][%d] number : ", row_loop, col_loop);
- scanf("%d", &rowptr[row_loop][col_loop]);
- }
- }
- // -----------------------------------------------------------
- // Enter the second Matrix
- printf("Enter the Second Matrix : \n");
- // Allocate memory for rowptr_1 **
- // if nrow == 3, then same as rowptr_1 *[3]
- rowptr_1 = (int **)malloc(sizeof(int **) * (*nrow_1ptr));
- if (rowptr_1 == NULL)
- {
- perror("Dynamic Memory Allocation Matrix 2 for row Fails");
- }
- // For each rowptr_1 * , allocate ncolumn
- for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
- {
- rowptr_1[row_loop] = (int *)malloc(sizeof(int *) * (*ncol_1ptr));
- if(rowptr_1[row_loop] == NULL)
- {
- perror("Dynamic Memory Allocation Matrix 2 for column Fails");
- }
- }
- for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
- {
- for (col_loop = 0; col_loop < (*ncol_1ptr); col_loop++)
- {
- printf("Enter the [%d][%d] number : ", row_loop, col_loop);
- scanf("%d", &rowptr_1[row_loop][col_loop]);
- }
- }
- // Check the Dimension of Two Matrix
- /* symmetric = Symmetric(nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
- if (symmetric == 1)
- {
- printf("The two Matrix is same dimension");
- }
- else
- {
- printf("The two Matrix is different dimension");
- }
- }while(symmetric == 0); */
- break;
- }
- }
- }
- // ------------------------------------------------------------
- int Symmetric(int *nrowptr, int *ncolptr,
- int *nrow_1ptr, int *ncol_1ptr)
- {
- int symmetric;
- if ((*nrowptr) == (*nrow_1ptr) && (*ncolptr) == (*ncol_1ptr))
- {
- symmetric = 1;
- return symmetric;
- }
- else
- {
- symmetric = 0;
- return symmetric;
- }
- }
- // ------------------------------------------------------------
- void Add(int **rowptr, int **rowptr_1, int *nrowptr, int *ncolptr)
- {
- static int **result;
- int row_loop, col_loop;
- // Allocate memory for result **
- // if nrow == 3, then same as result *[3]
- result = malloc(sizeof(int **) * (*nrowptr));
- if (result == NULL)
- {
- perror("Dynamic Memory Allocation result for row Fails");
- }
- // For each result * , allocate ncolumn
- for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
- {
- result[row_loop] = malloc(sizeof(int *) * (*ncolptr));
- if (result[row_loop] == NULL)
- {
- perror("Dynamic Memory Allocation result for column Fails");
- }
- }
- // Adding the element of two matrix
- for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
- {
- for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
- {
- *(*(result + row_loop) + col_loop) = *(*(rowptr + row_loop) + col_loop) + *(*(rowptr_1 + row_loop) + col_loop);
- }
- }
- Display(result, nrowptr, ncolptr);
- }
- // ------------------------------------------------------------
- void Subtract(int **rowptr, int **rowptr_1, int *nrowptr, int *ncolptr)
- {
- int row_loop, col_loop;
- static int **result;
- // Allocate memory for result **
- // if nrow == 3, then same as result *[3]
- result = malloc(sizeof(int **) * (*nrowptr));
- if (result == NULL)
- {
- perror("Dynamic Memory Allocation result for row Fails");
- }
- // For each result * , allocate ncolumn
- for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
- {
- result[row_loop] = malloc(sizeof(int *) * (*ncolptr));
- if (result[row_loop] == NULL)
- {
- perror("Dynamic Memory Allocation result for column Fails");
- }
- }
- // Subracting the element of two matrix
- for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
- {
- for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
- {
- *(*(result + row_loop) + col_loop) = *(*(rowptr + row_loop) + col_loop) - *(*(rowptr_1 + row_loop) + col_loop);
- }
- }
- Display(result, nrowptr, ncolptr);
- }
- // ------------------------------------------------------------
- void Display(int **result, int *nrowptr, int *ncolptr)
- {
- int row_loop, col_loop;
- // Loop for row and column
- for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
- {
- for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
- {
- printf("\nThe value of array at [%d][%d] is %d", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
- }
- }
- }
- // ------------------------------------------------------------
Your help is greatly appreciated by me and others.