MATRIX* init_matrix (MATRIX* m, int x, int y) {
/*
PURPOSE: Allocate memory for MATRIX m
ACCEPTS: m (MATRIX pointer)
x (int) num of rows
y (int) num of cols
RETURNS: NULL on error, m otherwise
REMARKS:
*/
int i; // loop index
// invalid dimensions
if (0 >= x || 0 >= y) {
return NULL;
}
free_matrix(m);
// allocate new array pointers
m->data = (double**)calloc(x, sizeof(double*));
if (NULL == m->data) {
return NULL;
}
// allocate data storage
for (i = 0; i < x; i++) {
m->data[i] = (double*)calloc(y, sizeof(double));
if (NULL == m->data[i]) {
return NULL;
}
}
// assign dimensions
m->rows = x;
m->cols = y;
// allocated successfully
return m;
}