08-31-2005 02:15 PM
08-31-2005 07:20 PM
Dave,
The reason 2) and 3) both work is that the memory layout is the same in both cases. This same behavior should be exhibited by any analysis matrix function. If you want to use a (double **) in your program, I have a suggestion:
Instead of allocating n "row" arrays, simply allocate one array and point into it from your double **.
ex.
int numRows = 10, numCols = 5, i;
double **matrix = malloc(numRows * sizeof(double *));
double *matrixData = malloc(numRows * numCols * sizeof(double));
for (i = 0; i < numRows; ++i)
matrix[i] = matrixData + i * numCols;
With this setup, you can use matrix[i][j] type notation freely; when you need to pass the array to an analysis function, simply pass (*matrix) which will be what the function expects.
Transpose(*matrix, numRows, numCols, *otherMatrix);
In addition to this approach transitioning easily between these idioms, it only does 2 memory allocations and is therefore faster than doing numRows + 1 allocations. It also makes cleaning up easier, as you only have to free *matrix and matrix; no loop is needed.
Hope this helps...
Regards,
-alex