The code was constructed using the property that the column of A and the row of B must be the same
to find the matrix product.
It worked when I multiplied the 2D matrix without using dynamic allocation, but it didn't work after using dynamic allocation with function. So I looked up the address, and the address was different by 30
.
int *matrixMulti(int N, int M, int L, int *A, int *B)
{
int *Mat = (int *)malloc(N * L * sizeof(int));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < L; j++)
{
*(Mat + i * M + j) = 0;
for (int k = 0; k < M; k++)
{
*(Mat + i * M + j) += A[i * M + k] * B[k * M + j];
}
printf("Mat[%d][%d] Mat[i][j] %d Mat address : %p \n", i, j, *(Mat + i * M + j), Mat);
printf("\n");
}
}
return Mat;
}
int main()
{
int N, M, L;
printf("input N M L\n ");
scanf("%d %d %d", &N, &M, &L);
int *A = (int *)malloc(N * M * sizeof(int));
int *B = (int *)malloc(L * M * sizeof(int));
int *S = (int *)malloc(N * L * sizeof(int));
input(N, M, A);
Array_print(N, M, A);
input(M, L, B);
Array_print(M, L, B);
S = matrixMulti(N, M, L, A, B);
printf("\n S address %p\n", matrixMulti(N, M, L, A, B)); ////////////////////////// ---> A
printf("\n S address %p\n", S); ////////////////////////// ---> A-30
// Array_print(N, L, matrixMulti(N, M, L, A, B));
free(A);
free(B);
free(S);
return 0;
}
At first, when only arrays
were passed and received, matrix multiplication was also output normally, but it became strange when changing to pointers
.
Output
S address 0x159f04120
S address 0x159f040f0
Expected
S address 0x159f04120
S address 0x159f04120
Full code here