automatic initialization of a structure to zero (smthg like a default constructor)

Hi,

when I define a structure, specialy those with pointers inside, I create a init function to set all members to zero or NULL. Then functions to allocate and deallocate memory for my structure can work well. In order to avoid confusion here is an example with matrices:

in matrix.h:

Code:

`typedef struct{`

int row,col;

double **mat;

}Matrix;

and in matrix.c:

Code:

`void initMatrix(Matrix *m){`

m->row = m->col = 0;

m->mat = NULL;

}

void freeMatrix(Matrix *m){

int i=0;

for( i=0; i<m->row; i++ )

free(m->mat[i])

free(m->mat);

initMatrix(m); //reset the state of the matrix

}

void allocMatrix(Matrix *m, int row, int col){

int i=0;

freeMatrix(m); //protect against double allocation

m->row = row;

m->col = col;

m->mat = (double **) malloc(sizeof(double *)*row);

for( i=0; i<row; i++ )

m->mat[i] = (double *) malloc(sizeof(double)*col);

}

and in the main source:

Code:

`void foo(){`

Matrix A;

init(&A);

allocMatrix(&A,100,100);

...

freeMatrix(&A);

}

my question here is: is there a way to avoid this fastidious initialization procedure and write code like the following?

Code:

`typedef struct{`

int row=0,col=0;

double **mat=NULL;

}Matrix;

void foo(){

Matrix A;

allocMatrix(&A,,100,100);

...

freeMatrix(&A);

}

The reason is that it is annoying to initialize when there are many matrices. Moreover it would prevent errors if the developer forgets to call the initFunction.

best regards to those who read till there

Brice