I'm working on my student project about dynamic allocation and I think I have a problem with my sending, someone can help?
void buildBoard(int*** mat, int size);
void initMat(int*** mat, int size);
int main()
{
int size;
int** mat;
printf("Please enter a size of the matrix:\n");
scanf("%d", &size);
buildBoard(&mat, size);
initMat(&mat, size);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
void buildBoard(int*** mat, int size)
{
*mat = (int**)malloc(size * sizeof(int*));
if (*mat == NULL)
{
printf("Bye\n");
exit(1);
}
for (int i = 0; i < size; ++i)
{
mat[i] = (int*)malloc(size * sizeof(int));
if (mat[i] == NULL)
{
printf("Bye\n");
free(mat[i]);
}
}
}
void initMat(int*** mat, int size)
{
int num;
printf("Please enter a numbers:\n");
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
scanf("%d", &mat[i][j]);
}
}
}
In the main function I want to check my allocation, and it crashes all the time on when printing.