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;
}
Quote:
Originally Posted by Sample output
How many matrices? 2
How many rows for matrix 1? 3
How many columns for matrix 1? 4
How many rows for matrix 2? 4
How many columns for matrix 2? 5
00 01 02 03
10 11 12 13
20 21 22 23

00 01 02 03 04
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34