typedef struct {
int num_rows;
int num_cols;
int** data;
} BinaryMatrix;
BinaryMatrix *ConstructBinaryMatrix(int num_rows, int num_cols) {
BinaryMatrix matrix = {
.num_rows = num_rows,
.num_cols = num_cols,
.data = (int **) malloc((num_rows) * sizeof(int *)),
};
int i;
for (i = 0; i < num_cols; i++) {
matrix.data[i] = (int *) malloc(num_cols * sizeof(int));
}
return &matrix;
}
Is this the correct way to define a BinaryMatrix, and how to initialize it?
Thanks for your help.
I got the following error.
BinaryMatrix* M;
M = ConstructBinaryMatrix(2, 2);
printf("%d:%d", M->num_rows, M->num_cols);
The output is: 4198012:0