struct matrix {
int m, n; // m rows, n columns
int** data;
char name[64];
};
struct matrix* alloc_matrix(const char* name, int m, int n) {
struct matrix *mat = malloc(sizeof(struct matrix));
strcpy(mat->name, name);
mat->m = m;
mat->n = n;
mat->data = (int**)malloc(sizeof(m) * (m * n));
return mat;
}
int main() {
struct matrix* mat1 = alloc_matrix("mat1", 4, 4);
mat1->data[0][0] = 2; <------------------------------ This line causes the error
return EXIT_SUCCESS;
}
So I want to implement matrices. I defined a struct matrix that holds the name, rows count, columns count and data of a matix. With a function alloc_matrix I want to allocade memory for a struct object. But something goes wrong in this function, because if I want to retrieve or set data on a allocated object, I get a runtime memory error.
I hope someone has more experience with dynamic data allocation and sees the problem.