Попробуйте код ниже (он работает на linux с g cc):
#include <stdio.h>
#include <stdlib.h>
void print_matrix(int *M, int r, int c) {
int i, j;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("%d ", *(M + i*c + j));
}
printf("\n");
}
}
int main (void) {
int i, j, temp;
unsigned int row1, row2, column1, column2;
printf ("Enter rows and columns of A matrix: ");
scanf ("%u%u", &row1, &column1);
int *A = (int *)malloc(row1 * column1 * sizeof(int));
printf ("Enter rows and columns of B matrix: ");
scanf ("%u%u", &row2, &column2);
int *B = (int *)malloc(row2 * column2 * sizeof(int));
printf ("Enter elements of A matrix\n");
for ( i = 0; i < row1; i++ ) {
for ( j = 0; j < column1; j++ ) {
scanf ("%d", &temp);
*(A + i*column1 + j) = temp;
}
}
printf ("\n\nEnter elements of B matrix\n");
for ( i = 0; i < row2; i++ ) {
for ( j = 0; j < column2; j++ ) {
scanf ("%d", &temp);
*(B + i*column2 + j) = temp;
}
}
printf("\n\nMatrix A:\n");
print_matrix( A, row1, column1);
printf("\n\nMatrix B:\n");
print_matrix( B, row2, column2);
}
ВЫХОД
$ ./m2
Enter rows and columns of A matrix: 2 3
Enter rows and columns of B matrix: 4 3
Enter elements of A matrix
1
2
3
4
5
6
Enter elements of B matrix
1
2
3
4
5
6
7
8
9
0
1
2
Matrix A:
1 2 3
4 5 6
Matrix B:
1 2 3
4 5 6
7 8 9
0 1 2