Quote Originally Posted by SingaBrice
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.
Then dump the initFunction completely, and just use the alloc:
Code:
Matrix *allocMatrix(int row, int col)
{
  Matrix  *m;

  m = malloc(sizeof(*m)); /* Error check! */

  int i = 0;
  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);
  }

  return(m);
}

void foo(void)
{
  Matrix  *A;
  A = allocMatrix(100, 100);
  ... 
  freeMatrix(A);
}
Also, if you want a safer way to initialise a newly malloc'd structure to have all zeros, use a static struct as a base, and copy it in. This way, if you add additional elements to the structure, you never need worry about their initialisation. Here's an example:
Code:
#include <stdio.h>

typedef struct
{
  int row;
  int col;
  double **mat;
} Foo;

void initFoo(Foo *f)
{
  const static Foo myFoo;
  *f = myFoo;
}

int main(void)
{
  Foo mainsFoo;
  
  printf ("mainsFoo, uninitialised: %d, %d, %p\n", 
		  mainsFoo.row,
		  mainsFoo.col,
		  mainsFoo.mat);

  initFoo(&mainsFoo);
  
  printf ("mainsFoo, initialised: %d, %d, %p\n", 
		  mainsFoo.row,
		  mainsFoo.col,
		  mainsFoo.mat);
			
  return(0);
}

/*

mainsFoo, uninitialised: 256, 1, 0040A054
mainsFoo, initialised: 0, 0, 00000000
  
*/