pass double pointer to function

Quote:

> The following code is part of my program. I don't know

> why I can't pass the array HWB[][] in a function Transpose

> (). Does anyone know why? I tried to display the values

> in mat[][] in the function but they are all zero. It

> seems HWB[][] hasn't passed to mat[][].

> /*HWB stored some values and all variables are declared*/

> double HWB[30][30];

> double **inarray;

> int main()

> {

> inarray=(double **)HWB;

> Transpose(inarray,Obsrows,CommonUnknown,outarray);

> }

> void Transpose(double **mat,int rowtran,int coltran,

> double **trans)

> {

> long p;

> long q;

> double *index[30];

> double outbuffer[30][30];

> for (p =0; p<rowtran;p++)

> {

> index[p]=(double *)mat + coltran*p;

> }

> for (p =0; p<rowtran;p++)

> {

> for (q =0;q< coltran;q++)

> {

> outbuffer[q][p] = index[p][q];

> }

> }

> trans=(double **)outbuffer;

> }

You need to prototype it like this

void Transpose(double (*mat)[30],int rowtran)

{

//...

Quote:

}

and you would pass HWB like

Transpose(HWB, 30)

Explaination:

C/C++ has only one-dimensional arrays. A two-dimensional arrays is actually

a one-dimensional array, in which each element is itself an array. A

declaration such as

int A[3][6];

declares array of three arrays each of which holds six ints. A is also

equivlent to a pointer. This pointer is of type "pointer to an array of six

ints". The typedef

typedef int (*pArray_of_six_ints)[6];

allows us to use pArray_of_six_ints as a synonym for "pointer to an array of

six ints". The paranthesis are necessary. Without them we declare a

different type, an array of six pointers to int. With pArray_of_six_ints we

can point to each row of A.

pArray_of_six_ints p1st_row = A; // same as &A[0]

pArray_of_six_ints p2nd_row = A+1; // same as &A[1]

pArray_of_six_ints p3rd_row = A+2; // same as &A[2]

Since p1st_row is a pointer to an array of six ints, *p1st_row is the actual

array of six ints, and (*p1st_row)[0] is the first element of that array.

Because the index operator [] is of higher precedence than the derefence

operator parentheses are required around *p1st_row.

If we write *p1st_row as *(p1st_row + 0), we see that *p1st_row is the same

thing as p1st_row[0]. So, (*p1st_row)[c], is the same thing as

(p1st_row[0])[c], except, now, the parentheses are no longer necessary.

Thus, (*p1st_row)[c] can be written as p1st_row[0][c], (*p2nd_row)[c] can

be written as p2n_row[0][c]. And (*p3rd_row)[c] as p3rd_row[0][c].

Since p1st_row + 1 is equal to p2nd_row, and p1st_row + 2 is equal to

p3rd_row, we see that p1st_row[i][j]is the same as A[i][j].

kurt