I have a struct like:
typedef struct {
int row;
int col;
double *entries;
} Matrix;
I want to write functions to create, free, etc. For example:
Matrix *mtalloc(typeof((Matrix) {}.row) row, typeof((Matrix) {}.col) col)
{
Matrix *p = malloc(sizeof(Matrix));
if (p == NULL) {
fprintf(stderr, "mtalloc(%x, %x): malloc returned NULLn", row, col);
exit(EXIT_FAILURE);
}
p->row = row;
p->col = col;
p->entries = malloc(sizeof(*((Matrix) {}.entries)) * row * col);
return p;
}
This way the function won’t have to be changed if the type of the fields in struct change but is it fine or bad practice?