Code:

#include<stdio.h>
/* Allocate memory for a mtrix of given order */
void create_matrix( int ***mat, int m, int n)
{
int **la = NULL;
int i;
la = (int**)malloc( m* sizeof(int*));
for ( i=0; i <m; i++ )
la[i] = (int *)malloc( n* sizeof(int));
*mat = la;
return;
}
/* free memory of matrix allocated in create_matrix() */
void free_matrix( int ***mat, int m)
{
int **la = NULL;
int i;
la = *mat;
for ( i=0; i <m; i++ )
free (la[i] );
free ( la );
*mat=NULL;
return;
}
/* Print a matrix of order (mxn) */
void print_matrix( int **mat, int m, int n)
{
int i,j;
printf("Matrix of order(%dx%d)\n",m,n);
printf("\n");
for ( i=0; i <m; i++ )
{
for ( j=0; j <n; j++ )
printf("%4d",mat[i][j]);
printf("\n");
}
return;
}
/* Read a matrix of order (mxn) */
void read_matrix( int **mat, int m, int n)
{
int i,j;
printf("\nEnter %d elemnts for row, %d times\n",n,m);
for ( i=0; i <m; i++ )
{
for ( j=0; j <n; j++ )
scanf("%d",&mat[i][j]);
}
return;
}
int main()
{
int i,j,k;
int **a,**b,**c;
int m1,n1; /* A of m1xn1 */
int m2,n2; /* B of m2xn2 */
int m3,n3; /* C of m3xn3 => m1Xn2 (?) */
while(1) {
printf("\nEnter order of the First matrix: ");
scanf("%d%d",&m1,&n1);
printf("\nEnter order of the Second matrix: ");
scanf("%d%d",&m2,&n2);
/* Check if n1 == m2 */
if ( m2 == n1 )
break;
printf("\nMatrices can not be multiplied.. Try again.. ");
}
m3 = m1;
n3 = n2;
/* Allocate memory for all three matrices */
create_matrix( &a, m1, n1 );
create_matrix( &b, m2, n2 );
create_matrix( &c, m3, n3 );
/* Read the matrices to be multiplied */
read_matrix( a, m1, n1 );
read_matrix( b, m2, n2 );
print_matrix( a, m1, n1 );
print_matrix( b, m2, n2 );
/* Multyply the matrices */
for ( i = 0; i < m3; i++ )
{
for ( j = 0; j < n3; j++ )
{
c[i][j] = 0;
for ( k = 0; k < m2; k ++ )
c[i][j] += a[i][k]*b[k][j] ;
}
}
print_matrix( c, m3, n3 );
free_matrix( &a, m1 );
free_matrix( &b, m2 );
free_matrix( &c, m3 );
return 0;
}