fala rapaziada, estou tentando criar um programa que recebe duas matrizes e calcula a multiplicação entre as duas, porém estou recebendo um erro de segmentation fault e não consigo mto bem entender o por que, alguém consegue me dar alguma luz?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int temp, m, n, p, q, c, d, k, sum = 0;
scanf("%d%d", &m, &n);
int **first = malloc(sizeof(int) * m);
for(int i = 0; i < m; i++)
{
first[i] = malloc(sizeof(int) * n);
}
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
scanf("%d", &first[c][d]);
}
}
scanf("%d%d", &p, &q);
int **second = malloc(sizeof(int) * p);
for(int i = 0; i < p; i++)
{
second[i] = malloc(sizeof(int) * q);
}
for ( c = 0 ; c < p ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
scanf("%d", &second[c][d]);
}
}
int **multiply = malloc(sizeof(int) * m);
for(int i = 0; i < m; i++)
{
multiply[i] = malloc(sizeof(int) * q);
}
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d ", multiply[c][d]);
printf("\n");
}
for(int i = 0; i < m; i++)
{
free(first[i]);
}
free(first);
for(int i = 0; i < p; i++)
{
free(second[i]);
}
free(second);
for(int i = 0; i < m; i++)
{
free(multiply[i]);
}
free(multiply);
return 0;