gnu.gcc.help
[Top] [All Lists]

Re: passing an array to a function using pointers

Subject: Re: passing an array to a function using pointers
From: "swagat" <swagat.kumar@xxxxxxxxx>
Date: 24 Nov 2005 07:49:48 -0800
Newsgroups: gnu.gcc.help
Dear Thomas,

I am certainly aware about

double (*q)[4]

But here we need to specify the second dimension of the array which may
not be known during run time.


double **q can be used in the function provided the original matrix is
created with a similar pointer.

I mean if we have created a matrix as :
  double **a;
  a=new double *[4];
  for(i=0;i<4;i++)
    a[i]=new double [4];

  for(i=1;i<=3;i++)
    for(j=1;j<=3;j++)
      a[i][j]=i*j;

then, we can write a display routine as follows:

void display_matrix(double **P, int row, int col)
{
   int i,j;
   for(i=0;i<row;i++){
      for(j=0;j<row;j++)
          cout<<p[i][j]<<"\t";
      cout<<endl;
  }
}
---------------------------------
Thirdly, I want to know how to call the template function from main
programme.
Can I call it as

display_matrix(a,3,4);

Why I need such a form is that I am overloading it with one which
displays a vector too. Evenif, i don't retain this form, how do I call
the template function? Sorry, for asking such dumb questions here, but
I really need to know them.

Thanks a lot for explanation.

regards,
swagat


<Prev in Thread] Current Thread [Next in Thread>