Here ya go:
Code:
/* Create a soon-to-be matrix of doubles, named dmatrix */
double **dmatrix;
int row;
/* Now we need to allocate N 'rows'. */
dmatrix = malloc( sizeof( double* ) * N );
/* Now, for each row, allocate M actual doubles. */
for( row = 0; row < N; row++ )
dmatrix[ row ] = malloc( sizeof( double ) * M );
/* Now you have a matrix of N rows by M colums worth of doubles. */
You can now use this as a 2D array in effect.
Code:
dmatrix[ foo ][ bar ] = 2.3;
To free what you've allocated, you do those two steps in reverse. You free each row's doubles, then you free the pointers (ie: rows)
Code:
for( row = 0; row < N; row++ )
free( dmatrix[ row ] ); /* Free this row's doubles. */
free( dmatrix ); /* Free the row holders. */
Quzah.