/*
This program demonstrates a technique for allocating
memory to a 2-dimensional matrix which allows you to
use double subscripting when the pointer to the
matrix is passed to a function.
*/
main()
{
double **x,*xnow;
long i,j;
long m=3,n=5;
double findelt(double **mat,long i,long j);
double findelt1(double *mat,long i,long j,long nc);
/*
As an example, a 3 x 5 matrix is allocated. Notice that
by allocating all the memory for x in a single malloc call
and then setting the row pointers, a dereferenced version
of the matrix (either *x or x[0]) can be passed to routines
which expect the matrix to be stored as a vector. One
caveat: FORTRAN stores it's matrices by columns, while this
scheme forces the matrix to be stored by rows. This needs
to be considered if any FORTRAN subroutines or functions are
used with matrices.
The first allocation only provides memory for the row pointers,
*not* for the actual data values which will be stored in x.
*/
x = (double**)malloc((unsigned)(m * sizeof(double*)));
/*
The allocation to xnow is for the entire m by n array - then the
previous allocated row pointers can be set to beginnings of each
of the m rows, by incrementing the xnow pointer by n elements
*/
xnow = (double*)malloc((unsigned)(m * n * sizeof(double)));
for(i=0;i<3;i++,xnow += n)x[i] = xnow;
/*
To test this technique, 10 * i + j is put into the i,j th
element of x, and then retreived via two different functions
*/
for(i=0;i