pass double pointer to function 
Author Message
 pass double pointer to function

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);

Quote:
}

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;
Quote:
}



Mon, 26 Sep 2005 02:10:16 GMT  
 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



Mon, 26 Sep 2005 04:12:21 GMT  
 pass double pointer to function
In addition to what Kurt mentioned you can get rid of the annoying
restriction on the size of the array using a template parameter.

template<int T>
void f(int (*array)[T], int nLen)
{

Quote:
}

int main()
{
    int a[10][20];
    f<20>(a, 10);

Quote:
}

--
sashan
http://sashan.netfirms.com
--------------------------------------------------
Brain: Here we are, Pinky--at the dawn of time!
Pinky:  Narf, Brain.  Wake me at the noon of time.


Mon, 26 Sep 2005 06:49:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing double pointer as argument to a function

2. Double indirection, pointer passing, FUBAR!

3. Pointer to function passed to varargs function

4. Novice: Passing a function pointer to a function

5. How to use (pointer to function), and function and pointer to (pointer to function)

6. Prototyping a function passed a pointer to a function

7. How to pass a member function as inetrnal function pointer argument

8. Novice wants to pass type double to function

9. double pointer (or pointer to pointer)

10. Pointer Functions and Pointers to Pointer Functions

11. double pointer to function

12. Pointer-to-Double as Function Arg

 

 
Powered by phpBB® Forum Software