Dynamic Memory Allocation for Matrix(2D Array)

Discussion in 'C' started by Peter_APIIT, Apr 16, 2007.

  1. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Hello all expert C programmer especially shabbir,

    i have a question which bring my emotion from happy to unhappy. My program is a use of techniques dynamic memory allocation to alocate the memory for 2D array. I have successfully allocating the memory for 2D but when i try to add two matrix, the program just display an error message which force my program terminated.

    I know this situation is related to run time error. I try to debug and catch the errors. Unfortunately, my effort doesn't bring any desires results.

    I hope that someone which kind enough to help me in this problem.

    Below is my code:

    Code:
    /* 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 do 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
    */
    int Symmetric(int, int, int, int);
    void Add(int **, int **, 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[])
    {
    	int nMatrix;
    	int nrow, ncol, nrow_1, ncol_1;
    	// Number of row and column
    	
    	int row_loop, col_loop;
    	// Loop for row and column
    
    	int **rowptr = NULL;   // -- First Matrix
    	// A pointer to pointer to integer
    
    	int **rowptr_1 = NULL; // -- Second Matrix
    	int **result = NULL;  // Result of two Matrix
    	int symmetric;
    	/*
    	   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);
    		switch(nMatrix)
    		{
    		    case 1:
    			{
    				printf("How many Row : ");
    				scanf("%d", &nrow);
    				rowptr = malloc(sizeof(int) * nrow);
    				if (rowptr == NULL)
    				{
    					perror("Dynamic Memory Allocation for row Fails");
    				}	
    				printf("How many Column : ");					scanf("%d", &ncol);
    
    				for (row_loop=0;row_loop<nrow;row_loop++)
    				{
    					rowptr[row_loop] = malloc(sizeof(int) * ncol);
    					if (rowptr[row_loop] == NULL)
    					{	
    						perror("Dynamic Memory Allocation for column Fails");
    					}
    				}
    
    				for (row_loop=0;row_loop<nrow;row_loop++)
    				{
    					for (col_loop=0;col_loop<ncol;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("Enter the First Matrix : \n");
    					printf("How many Row : ");
    					scanf("%d", &nrow);
    					rowptr = malloc(sizeof(int) * nrow);
    					if (rowptr == NULL)
    					{
    						perror("Dynamic Memory Allocation for row Fails");
    					}	
    					printf("How many Column : ");
    					scanf("%d", &ncol);
    
    					for (row_loop=0;row_loop<nrow;row_loop++)
    					{
    						rowptr[row_loop] = malloc(sizeof(int) * ncol);
    						if (rowptr[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation for column Fails");
    						}
    					}
    
    					for (row_loop=0;row_loop<nrow;row_loop++)
    					{
    						for (col_loop=0;col_loop<ncol;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");
    					printf("How many row : ");
    					scanf("%d", &nrow_1);
    					rowptr_1 = malloc(sizeof(int) * nrow_1);
    					if (rowptr_1 == NULL)
    					{
    						perror("Dynamic Memory Allocation Matrix 2 for row Fails");
    					}
    					printf("How many Column : ");
    					scanf("%d", &ncol_1);
    
    					for (row_loop=0;row_loop<nrow_1;row_loop++)
    					{
    						rowptr_1[row_loop] = malloc(sizeof(int) * nrow_1);
    						if(rowptr_1[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation Matrix 2 for column Fails");
    						}
    					}
    					
    					for (row_loop=0;row_loop<nrow_1;row_loop++)
    					{
    						for (col_loop=0;col_loop<ncol_1;col_loop++)
    						{
    							printf("Enter the [%d][%d] number : ", row_loop, col_loop);
    							scanf("%d", &rowptr_1[row_loop][col_loop]);
    						}
    					}
    					break;
    				}while(symmetric == 0);
    			}
    		}
    // ------------------------------------------------------------				
    		// Check the Dimension of Two Matrix
    		symmetric = Symmetric(nrow, ncol, nrow_1, ncol_1);
    		if (symmetric == 1)
    		{
    			printf("The two Matrix is same dimension");
    		}
    		else
    		{
    			printf("The two Matrix is different dimension");
    		}
    
                   // Runtime Error
    		Add(rowptr, rowptr_1, nrow, ncol, nrow_1, ncol_1);
    		// Adding 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;
    
    }
    // -----------------------------------------------------------
    int Symmetric(int nrow, int ncol, int nrow_1, int ncol_1)
    {
    	int symmetric;
    	if (nrow == nrow_1 && ncol == ncol_1)
    	{
    		symmetric = 1;
    		return symmetric;
    	}
    	else
    	{
    		symmetric = 0;
    		return symmetric;
    	}
    }
    // ------------------------------------------------------------	
    void Add(int **rowptr, int **rowptr_1, int nrow, 
    		 int ncol, int nrow_1, int ncol_1)
    {
    	int **result = NULL;
    	int row_loop, col_loop;
    	nrow = nrow_1;
    	ncol = ncol_1;
    
    	for (row_loop = 0;row_loop < nrow; row_loop++)
    	{
    		for (col_loop = 0; col_loop < ncol; col_loop++)
    		{
    			*(*(result + row_loop) + col_loop) =  (*(*(rowptr + row_loop) + col_loop))   +  (*(*(rowptr_1 + row_loop) + col_loop));
    		}
    	}
    
    }
    
    // ------------------------------------------------------------
    void Display(int **result, int nrow, int ncol)
    {
    	int row_loop, col_loop;
    	// Loop for row and column
    
    	for (row_loop = 0; row_loop < nrow; row_loop++)
    	{
    		for (col_loop = 0; col_loop < ncol; col_loop++)
    		{
    			printf("The value of array at [%d][%d] is %d\n", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
    		}
    	}
    
    }
    
    // ------------------------------------------------------------
    
    
    
    The error is appear after i check the dimension of the 2D array.

    Thanks for your help.

    Your help is greatly appreciated by me and others.
     
  2. ahayes

    ahayes New Member

    Joined:
    Mar 27, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    nrow_1 needs to be ncol_1

    Code:
    for (row_loop=0;row_loop<nrow_1;row_loop++)
    					{
    						rowptr_1[row_loop] = malloc(sizeof(int) * nrow_1); //<--error is here
    						if(rowptr_1[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation Matrix 2 for column Fails");
    						}
    					}
    
    You are not allocating memory for the result matrix so you need to make this modification

    Code:
    void Add(int **rowptr, int **rowptr_1, int nrow, 
    		 int ncol, int nrow_1, int ncol_1)
    {
    	int **result = NULL;
    	int row_loop, col_loop;
    	nrow = nrow_1;
    	ncol = ncol_1;
    	
    	result = malloc(sizeof(int*) * nrow_1);
    					if (rowptr_1 == NULL)
    					{
    						perror("Dynamic Memory Allocation Matrix Result for row Fails");
    					}
    
    					for (row_loop=0;row_loop<nrow_1;row_loop++)
    					{
    						result[row_loop] = malloc(sizeof(int) * ncol_1);
    						if(rowptr_1[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation Matrix Result for column Fails");
    						}
    					}
    
    	for (row_loop = 0;row_loop < nrow; row_loop++)
    	{
    		for (col_loop = 0; col_loop < ncol; col_loop++)
    		{
    			*(*(result + row_loop) + col_loop) =  (*(*(rowptr + row_loop) + col_loop))   +  (*(*(rowptr_1 + row_loop) + col_loop));
    		}
    	}
    
    }
    also, when you are allocating for rows you need to have sizeof( (*int) * nrows) and not just sizeof((int)*nrows)
     
  3. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Hey man, giving a first look to your code it looked as if you are well versed i programming, but after thoroughly going i thought either you are not clear with mem managament concepts of have just copied code.
    i have made certain change that ececute code correctly.
    Many Confusing comments were given...
    I am still not able to figure the need of them...
    Also many unnecessary inputs were asked....
    and theres no ned to take symmetric matrix, any matrix with resultant matrix equal to size of largest matrix would solve the problem...
    Go through changes and understand..
    Also try to use code that others should feel easy to understand....


    Code:
    /* 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 do code this in C
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<assert.h>
    #include"conio.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
    */
    int Symmetric(int, int, int, int);
    void Add(int **, int **, 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[])
    {
    	clrscr();
    	int nMatrix;
    	int nrow, ncol, nrow_1, ncol_1;
    	// Number of row and column
    	
    	int row_loop, col_loop;
    	// Loop for row and column
    
    	int **rowptr = NULL;   // -- First Matrix
    	// A pointer to pointer to integer
    
    	int **rowptr_1 = NULL; // -- Second Matrix
    	int **result = NULL;  // Result of two Matrix
    	int symmetric;
    	/*
    	   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);
    		switch(nMatrix)
    		{
    		    case 1:
    			{
    				printf("How many Row : ");
    				scanf("%d", &nrow);
    				rowptr = (int **)malloc(sizeof(int) * nrow);
    				if (rowptr == NULL)
    				{
    					perror("Dynamic Memory Allocation for row Fails");
    				}
    				printf("How many Column : ");					scanf("%d", &ncol);
    
    				for (row_loop=0;row_loop<nrow;row_loop++)
    				{
    					rowptr[row_loop] = (int *)malloc(sizeof(int) * ncol);
    					if (rowptr[row_loop] == NULL)
    					{
    						perror("Dynamic Memory Allocation for column Fails");
    					}
    				}
    
    				for (row_loop=0;row_loop<nrow;row_loop++)
    				{
    					for (col_loop=0;col_loop<ncol;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("Enter the First Matrix : \n");
    					printf("How many Row : ");
    					scanf("%d", &nrow);
    					rowptr = (int **)malloc(sizeof(int) * nrow);
    					if (rowptr == NULL)
    					{
    						perror("Dynamic Memory Allocation for row Fails");
    					}
    					printf("How many Column : ");
    					scanf("%d", &ncol);
    
    					for (row_loop=0;row_loop<nrow;row_loop++)
    					{
    						rowptr[row_loop] =(int *) malloc(sizeof(int) * ncol);
    						if (rowptr[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation for column Fails");
    						}
    					}
    
    					for (row_loop=0;row_loop<nrow;row_loop++)
    					{
    						for (col_loop=0;col_loop<ncol;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");
    					printf("How many row : ");
    					scanf("%d", &nrow_1);
    					rowptr_1 = (int **)malloc(sizeof(int) * nrow_1);
    					if (rowptr_1 == NULL)
    					{
    						perror("Dynamic Memory Allocation Matrix 2 for row Fails");
    					}
    					printf("How many Column : ");
    					scanf("%d", &ncol_1);
    
    					for (row_loop=0;row_loop<nrow_1;row_loop++)
    					{
    						rowptr_1[row_loop] = (int*)malloc(sizeof(int) * nrow_1);
    						if(rowptr_1[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation Matrix 2 for column Fails");
    						}
    					}
    
    					for (row_loop=0;row_loop<nrow_1;row_loop++)
    					{
    						for (col_loop=0;col_loop<ncol_1;col_loop++)
    						{
    							printf("Enter the [%d][%d] number : ", row_loop, col_loop);
    							scanf("%d", &rowptr_1[row_loop][col_loop]);
    						}
    					}
    					break;
    				}while(symmetric == 0);
    			}
    		}
    // ------------------------------------------------------------
    		// Check the Dimension of Two Matrix
    		symmetric = Symmetric(nrow, ncol, nrow_1, ncol_1);
    		if (symmetric == 1)
    		{
    			printf("The two Matrix is same dimension");
    		}
    		else
    		{
    			printf("The two Matrix is different dimension");
    		}
    
    	       // Runtime Error
    		Add(rowptr, rowptr_1, nrow, ncol, nrow_1, ncol_1);
    		// Adding 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;
    
    }
    // -----------------------------------------------------------
    int Symmetric(int nrow, int ncol, int nrow_1, int ncol_1)
    {
    	int symmetric;
    	if (nrow == nrow_1 && ncol == ncol_1)
    	{
    		symmetric = 1;
    		return symmetric;
    	}
    	else
    	{
    		symmetric = 0;
    		return symmetric;
    	}
    }
    // ------------------------------------------------------------
    void Add(int **rowptr, int **rowptr_1, int nrow,
    		 int ncol, int nrow_1, int ncol_1)
    {
    	int **result = NULL;
    	result= (int **)malloc(sizeof(int) * nrow * ncol);
    	int row_loop, col_loop;
    	nrow = nrow_1;
    	ncol = ncol_1;
    
    	for (row_loop = 0;row_loop < nrow; row_loop++)
    	{
    		for (col_loop = 0; col_loop < ncol; col_loop++)
    		{
    //			*(*(result + row_loop) + col_loop) =  (*(*(rowptr + row_loop) + col_loop))   +  (*(*(rowptr_1 + row_loop) + col_loop));
    			 result[row_loop][col_loop]=rowptr[row_loop][col_loop]+rowptr_1[row_loop][col_loop];
    		}
    	}
           Display(result,nrow,ncol);
    
    }
    
    // ------------------------------------------------------------
    void Display(int **result, int nrow, int ncol)
    {
    	int row_loop, col_loop;
    	// Loop for row and column
    
    	for (row_loop = 0; row_loop < nrow; row_loop++)
    	{
    		for (col_loop = 0; col_loop < ncol; col_loop++)
    		{
    			printf("The value of array at [%d][%d] is %d\n", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
    		}
    	}
    
    }
    
    // ------------------------------------------------------------
     
  4. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    I font understand whether you guys are actually famaliar with synataxes.
    while allocating you should know that malloc returns null pointer so that has to be typecasted.
    n you people straightway write mlloc(...);
    Try not to repeat. Any newbie reading this would get confused and get ample errors to get his head scratched...
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Malloc only returns a NULL pointer if it fails. Otherwise, in C (not C++), it returns a void pointer which needs to be cast.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Probably I am the last one to be replying but I would suggest or expect not to direct to any one but to the programmers as a whole.
     
  7. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    For the older compiler(K&R), you need cast the void pointer to the type data you pointed to but for modern compiler, you doesn't need to cast the void pointer to the type of data the pointing to.

    I don't know whether this is true. I just guess. I abg your pardon if my opinion make you angry.

    Thanks for all replies.

    Your help and replies is greatly appreciated by me and others.
     
  8. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Sorry shabbir, i won't do that again. Thank for your remind.
     
  9. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Hello all, i have successfully run the program but i not really understand two statement in the program.

    The two statement is as below;

    1. rowptr = (int **)malloc(sizeof(int **) * nrow);

    Allocate nrow of array of pointer - where if nrow = 5.
    The above statement is same as int *rowptr[5]; -- I don't know whether is it correct or not.

    2. for (row_loop=0;row_loop<nrow;row_loop++)
    {
    rowptr[row_loop] = (int *)malloc(sizeof(int *) * ncol);
    }

    For each rowptr, allocate int * pointer pointing to ncol.

    I don't know whether my understanding is correct or not. Please confirm with me whether my understanding is correct or not.


    Thanks for your help.
    Your help is greatly appreciated by me and others.

    I don't know how to return the help you all have give. By the way, i just know say thanks to you all. Without you all, i think i not able to solve this program. This program is not assignment nor project. I just practiced it on my own.

    Thanks.
    Thanks.
    Thanks.
    Thanks.
    Thanks.
    Thanks.
    Thanks.
    Thanks.
     
  10. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Ya thanks DaWei, i meant to say void pointer, but was just a mistake that i wrote null pointer. Thanks for correction..
     
  11. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Hello all, i have successfully solved the problem. Below is my code:

    Code:
    /* 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 do 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
    */
    int Symmetric(int, int, int, int);
    void Add(int **, int **, 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[])
    {
    
    	int nMatrix;
    	int nrow, ncol, nrow_1, ncol_1;
    	// Number of row and column
    	
    	int row_loop, col_loop;
    	// Loop for row and column
    
    	int **rowptr = NULL;   // -- First Matrix
    	// A pointer to pointer to integer
    
    	int **rowptr_1 = NULL; // -- Second Matrix
    	int **result = NULL;  // Result of two Matrix
    	int symmetric;
    	/*
    	   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);
    		switch(nMatrix)
    		{
    		    case 1:
    			{
    				printf("How many Row : ");
    				scanf("%d", &nrow);
    				rowptr = (int **)malloc(sizeof(int **) * nrow);
    				if (rowptr == NULL)
    				{
    					perror("Dynamic Memory Allocation for row Fails");
    				}
    				printf("How many Column : ");					
    				scanf("%d", &ncol);
    				// For each row, allocated ncol
    				for (row_loop=0;row_loop<nrow;row_loop++)
    				{
    					rowptr[row_loop] = (int *)malloc(sizeof(int *) * ncol);
    					if (rowptr[row_loop] == NULL)
    					{
    						perror("Dynamic Memory Allocation for column Fails");
    					}
    				}
    
    				for (row_loop=0;row_loop<nrow;row_loop++)
    				{
    					for (col_loop=0;col_loop<ncol;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("Enter the First Matrix : \n");
    					printf("How many Row : ");
    					scanf("%d", &nrow);
    					rowptr = (int **)malloc(sizeof(int **) * nrow);
    					if (rowptr == NULL)
    					{
    						perror("Dynamic Memory Allocation for row Fails");
    					}
    					printf("How many Column : ");
    					scanf("%d", &ncol);
    
    					for (row_loop=0;row_loop<nrow;row_loop++)
    					{
    						rowptr[row_loop] =(int *) malloc(sizeof(int *) * ncol);
    						if (rowptr[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation for column Fails");
    						}
    					}
    
    					for (row_loop=0;row_loop<nrow;row_loop++)
    					{
    						for (col_loop=0;col_loop<ncol;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");
    					printf("How many row : ");
    					scanf("%d", &nrow_1);
    					rowptr_1 = (int **)malloc(sizeof(int **) * nrow_1);
    					if (rowptr_1 == NULL)
    					{
    						perror("Dynamic Memory Allocation Matrix 2 for row Fails");
    					}
    					printf("How many Column : ");
    					scanf("%d", &ncol_1);
    
    					for (row_loop=0;row_loop<ncol_1;row_loop++)
    					{
    						rowptr_1[row_loop] = (int *)malloc(sizeof(int *) * ncol_1);
    						if(rowptr_1[row_loop] == NULL)
    						{
    							perror("Dynamic Memory Allocation Matrix 2 for column Fails");
    						}
    					}
    
    					for (row_loop=0;row_loop<nrow_1;row_loop++)
    					{
    						for (col_loop=0;col_loop<ncol_1;col_loop++)
    						{
    							printf("Enter the [%d][%d] number : ", row_loop, col_loop);
    							scanf("%d", &rowptr_1[row_loop][col_loop]);
    						}
    					}
    					break;
    				}while(symmetric == 0);
    			}
    		}
    // ------------------------------------------------------------
    		// Check the Dimension of Two Matrix
    		symmetric = Symmetric(nrow, ncol, nrow_1, ncol_1);
    		if (symmetric == 1)
    		{
    			printf("\nThe two Matrix is same dimension");
    		}
    		else
    		{
    			printf("\nThe two Matrix is different dimension");
    		}
    
    	       // Runtime Error
    		Add(rowptr, rowptr_1, nrow, ncol, nrow_1, ncol_1);
    		// Adding 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;
    
    }
    // -----------------------------------------------------------
    int Symmetric(int nrow, int ncol, int nrow_1, int ncol_1)
    {
    	int symmetric;
    	if (nrow == nrow_1 && ncol == ncol_1)
    	{
    		symmetric = 1;
    		return symmetric;
    	}
    	else
    	{
    		symmetric = 0;
    		return symmetric;
    	}
    }
    // ------------------------------------------------------------
    void Add(int **rowptr, int **rowptr_1, int nrow,
    		 int ncol, int nrow_1, int ncol_1)
    {	
    	int row_loop, col_loop;
    	int **result = NULL;
    	
    	nrow = nrow_1;
    	ncol = ncol_1;
    
    	result= (int **)malloc(sizeof(int **) * nrow);
    
    	for (row_loop = 0; row_loop < nrow; row_loop++)
    	{
    		result[row_loop] = (int *)malloc(sizeof(int *) * ncol);
    	}
    	if (result == NULL)
    	{
    		perror("Dynamic Memory Allocation for result Fails");
    	}
    	
    	for (row_loop = 0;row_loop < nrow; row_loop++)
    	{
    		for (col_loop = 0; col_loop < ncol; col_loop++)
    		{
    //			*(*(result + row_loop) + col_loop) =  (*(*(rowptr + row_loop) + col_loop))   +  (*(*(rowptr_1 + row_loop) + col_loop));
    			 result[row_loop][col_loop]=rowptr[row_loop][col_loop]+rowptr_1[row_loop][col_loop];
    		}
    	}
           Display(result,nrow,ncol);
    
    }
    
    // ------------------------------------------------------------
    void Display(int **result, int nrow, int ncol)
    {
    	int row_loop, col_loop;
    	// Loop for row and column
    
    	for (row_loop = 0; row_loop < nrow; row_loop++)
    	{
    		for (col_loop = 0; col_loop < ncol; col_loop++)
    		{
    			printf("The value of array at [%d][%d] is %d\n", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
    		}
    	}
    
    }
    
    // ------------------------------------------------------------ 
     
    and this is improved version of Matrix but have a run time error as well:
    
    
    Code:
    /* 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
    */
    void Allocate(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, nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
    		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;
    
    }
    // -----------------------------------------------------------
    void Allocate(int nMatrix, int *nrowptr, int *ncolptr,
    			  int *nrow_1ptr, int *ncol_1ptr)
    {
    	int symmetric;
    	int row_loop, col_loop;
    	static int **rowptr;
    	static int **rowptr_1;
    
    	switch(nMatrix)
    	{
    		case 1:
    		{
    			rowptr = 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] = 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 = 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] = 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 = 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] = 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 = NULL;
    	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 = NULL;
    
    
    	// 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) );
    		}
    	}
    
    }
    
    // ------------------------------------------------------------
    
    
    My idea is like this. I would like to call all function from main and pass the necessary to the function. Unfortunately, this is not i desired results i want.

    Thanks for your help.

    Your help is greatly appreciated by me and others.
     
  12. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Please help me. Thanks in advance for your help.
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Why don't you have a separate thread for each of your query as that helps someone else who is searching.
     
  14. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    I think this is the same thread and is a improve version of pervious version. I don' want to make others person confused and easy to search this topic.
     
  15. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Please help me. I terribly suffered right now.

    Thanks for any help.
     
  16. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Please help us as to what you need from us.
     
  17. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You've tossed a LOT of code out there, but you haven't given us any information about the kind of problem you're having, except "run time error". You don't give any indication of where that error might be occurring. You are expecting us to help you, but you won't give us any help. Please see the tips that I posted in your other thread.

    I could copy your code and paste it into my compiler and debug it for you and rewrite it correctly. You will learn more if you analyze your code carefully enough to EXPLAIN to us where we might concentrate our attention and avoid wasting our time on working areas.
     
  18. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    [comment]
    We can have this added in the [thread=168]Before you make a query[/thread]. I have opened the thread for you to paste it there.[/comment]
     
  19. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Thanks for all the reply.

    Fine. My problem is i have two program which the first program compiled and run without error but the second program cause an run time error. The problem for the second program is when the allocate has successfully allocate the memory for rowptr and rowptr_1. When the allocate function finish, rowptr and rowptr_1 become NULL at main.

    Thanks for your help.
     
  20. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You have a number of issues, one of which is that you need to spend more design time before you sit down at the keyboard.

    You will notice that I have converted some replicated variables to two-element arrays (two matrices). This allows me to reuse code by putting it in a loop and performing the actions as dictated by the array index. You may also notice that I could have carried this a little farther by also applying it to rowPtr0 and rowPtr1. I'm sure the duplicate code stands out.

    Please note that you should ALWAYS test scanf's return value. You are asking for two things: input, AND that the input represents a number. This requires a conversion. If the user enters "Z", scanf will fail. If you don't catch this, your program will fail, sometimes with a crash. ALWAYS TEST THE SUCCESS OF THE FUNCTIONS YOU USE.

    It is not necessary to cast the return of malloc in C (it is in C++). C can intrinsically convert a void pointer to any other type. Casting may actually conceal an error.

    Note that I terminate the program in case of an error (after freeing previously allocated things, if that has occurred). You may, of course, add code to try to recover.

    Note that I did not free ALL the memory I allocated. I leave that as an exercise for the user.

    This code has not been thoroughly tested.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int badNews (char *trouble)
    {
    	fprintf (stderr, "%s\n", trouble);
    	return -1;
    }
    int main (int argc, char *argv[])
    {
    	int i, j;
    	int mallocFail = 0;
    	int nMatrix;
    	int nRows [2];
    	int nCols [2];
    
    	int **rowPtr0, **rowPtr1;
    
    	printf ("How many matrices? ");
    	if (scanf ("%d", &nMatrix) != 1) return badNews ("Invalid input");
    	if (nMatrix != 1) nMatrix = 2;
    	for (i = 0; i < nMatrix; ++i)
    	{
    		printf ("How many rows for matrix %d? ", i+1);
    		if (scanf ("%d", &nRows [i]) != 1) return badNews ("Invalid input");
    		printf ("How many columns for matrix %d? ", i+1);
    		if (scanf ("%d", &nCols [i]) != 1) return badNews ("Invalid input");
    	}
    	if (nMatrix == 1)
    	{
    		printf ("Two matrices of the same size will be constructed\n");
    		nRows [1] = nRows [0];
    		nCols [1] = nCols [0];
    	}
    	/* First, storage for the row pointers */
    	rowPtr0 = malloc (sizeof (*rowPtr0) * nRows [0]);
    	if (!rowPtr0) mallocFail = 1;
    	else 
    	{
    		rowPtr1 = malloc (sizeof (*rowPtr1) * nRows [1]);
    		if (!rowPtr1) 
    		{
    			mallocFail = 1;
    			free (rowPtr0);
    		}
    	}
    	if (mallocFail) return badNews ("Malloc failed");
    
    	/* Null the pointers */
    	memset (rowPtr0, 0, sizeof (*rowPtr0) * nRows [0]);
    	memset (rowPtr1, 0, sizeof (*rowPtr1) * nRows [1]);
    
    	/* Now, the storage */
    	for (i = 0; (i < nRows [0]) && (!mallocFail); ++i)
    	{
    		rowPtr0 [i] = malloc (nCols [0] * sizeof (*rowPtr0 [i]));
    		if (!rowPtr0 [i]) mallocFail = 1;
    	}
    	for (i = 0; (i < nRows [1]) && (!mallocFail); ++i)
    	{
    		rowPtr1 [i] = malloc (nCols [1] * sizeof (*rowPtr1 [i]));
    		if (!rowPtr1 [i]) mallocFail = 1;
    	}
    	if (mallocFail)
    	{
    		/* 
    		Okay to call 'free' on NULL pointers, as well as
    		those which were allocated.
    		*/
    		for (i = 0; i < nRows [0]; ++i) free (rowPtr0 [i]);
    		for (i = 0; i < nRows [1]; ++i) free (rowPtr1 [i]);
    		return badNews ("Malloc failed");
    	}
    	/* Fill the matrices */
    	for (i = 0; i < nRows [0]; ++i)
    	{
    		for (j = 0; j < nCols [0]; ++j)
    		{
    			rowPtr0[i][j] = i*10 + j;
    		}
    	}
    	for (i = 0; i < nRows [1]; ++i)
    	{
    		for (j = 0; j < nCols [1]; ++j)
    		{
    			rowPtr1[i][j] = i*10 + j;
    		}
    	}
    	/* Show the matrices */
    	for (i = 0; i < nRows [0]; ++i)
    	{
    		for (j = 0; j < nCols [0]; ++j)
    		{
    			printf ("%02d ", rowPtr0[i][j]);
    		}
    		printf ("\n");
    	}
    	printf ("\n");
    
    	for (i = 0; i < nRows [1]; ++i)
    	{
    		for (j = 0; j < nCols [1]; ++j)
    		{
    			printf ("%02d ", rowPtr1[i][j]);
    		}
    		printf ("\n");
    	}
    	printf ("\n");
    
    	for (i = 0; i < nRows [0]; ++i) free (rowPtr0 [i]);
    	for (i = 0; i < nRows [1]; ++i) free (rowPtr1 [i]);
    	return 0;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice