I am still getting a hang of using malloc, calloc or realloc. Pretty sure I'm getting a segmentation fault because of an incorrect pointer or something, but for the life of me, I don't understand where I'm doing it wrong. The program works when the user input is '5 5', anything other than that causes the program to act weird and often times fail with a segmentation fault. Matrix A and B should be filled with random numbers, and Matrix C should sum matrix A and B, all the while using dynamic memory allocation. I'm not familiar with allocs, I've just started to understand pointers and 2D arrays. Can somebody explain what I'm doing wrong. Here's the whole code.
#include <stdio.h>
#include <stdlib.h>
#define ROWS 10
#define COLUMNS 10
void fillMatrix (int *matrix, int rows, int columns)
{
int i, j;
for(i = 0; i < rows; i++)
{
for(j= 0; j < columns; j++)
{
matrix[i * COLUMNS + j] = (rand() % 10) + 1;
}
}
}
void printMatrix (int *matrix, int rows, int columns)
{
int i, j;
for(i = 0; i < rows; i++)
{
for(j= 0; j < columns; j++)
{
printf("%2d ", *(matrix + i * COLUMNS + j));
}
printf("\n\n");
}
}
void sumMatrix (int *matrix, int *matrixA, int *matrixB, int rows, int columns)
{
int i, j;
for(i = 0; i < rows; i++)
{
for(j= 0; j < columns; j++)
{
matrix[i * COLUMNS + j] = matrixA[i * COLUMNS + j] + matrixB[i * COLUMNS + j];
}
}
}
int main()
{
int *matrixA = NULL, *matrixB = NULL, *matrixC = NULL;
int matrix[ROWS][COLUMNS], r, s, i;
srand((unsigned) time(NULL));
do{
printf("Rows and columns (min 1 1, max 10 10): ");
scanf("%d %d", &r, &s);
}while(r > ROWS || r < 1 || s > COLUMNS || s < 1);
matrixA = malloc(r * sizeof(int *));
for (i = 0; i < r; i++)
matrixA[i] = malloc(s * sizeof(int));
printf("\nMatrix A:\n\n");
fillMatrix(matrixA, r, s);
printMatrix(matrixA, r, s);
matrixB = calloc(r, sizeof(int *));
for (i = 0; i < r; i++)
matrixB[i] = calloc(s, sizeof(int));
printf("\nMatrix B:\n\n");
fillMatrix(matrixB, r, s);
printMatrix(matrixB, r, s);
matrixC = calloc(r, sizeof(int *));
for (i = 0; i < r; i++)
matrixC[i] = calloc(s, sizeof(int));
printf("\nSummed up (Matrix C):\n\n");
sumMatrix(matrixC, matrixA, matrixB, r, s);
printMatrix(matrixC, r, s);
free(matrixA);
free(matrixB);
free(matrixC);
return 0;
}
int**(int **matrixA = NULL, **matrixB = NULL, **matrixC = NULL;)