home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!julienas!sophia!xihu.inria.fr!gchow
- From: gchow@xihu.inria.fr (Gloria Chow)
- Newsgroups: comp.lang.c++
- Subject: Re: Passing 2-d arrys to functions
- Message-ID: <38049@sophia.inria.fr>
- Date: 11 Jan 93 13:58:08 GMT
- References: <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk> <RUNE.93Jan10125255@pandora.nta.no> <1993Jan11.030238.6688@nuscc.nus.sg>
- Sender: news@sophia.inria.fr
- Organization: INRIA, Sophia-Antipolis (Fr)
- Lines: 88
-
- In article <1993Jan11.030238.6688@nuscc.nus.sg>, suresh@argon.iss.nus.sg (Suresh Thennarangam - Research Scholar) writes:
- |> In article <RUNE.93Jan10125255@pandora.nta.no> rune@nta.no (Rune Henning Johansen FBA) writes:
- |> >In article <726521188snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk (Nikki Locke) writes:
- |> >
- |> > > For a two (or more) dimensional array, to access element [m][n], the
- |> > > compiler needs to know the address of the first element, the size of each
- |> > > element, and the number of elements in the least significant dimension.
- |> > > The calculation for an array declared [DIM1][DIM2] is
- |> > > ((m * DIM2 + n) * sizeof(element)).
- |> >
- |> >I'm including a little program that seems to work fine. Here I'm pas-
- |> >sing only the pointer to a two dimensional array. Can I expect such
- |> >programs to work, and if so, why?
- |> >
- |> > - Rune
- |> >
- |> > = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- |> >
- |> >#include <stream.h>
- |> >
- |> >#define begin {
- |> >#define end }
- |> >
- |> >void Input ( double **b )
- |> >begin
- |> > b[0][0] = 1.0;
- |> > b[1][0] = 2.0;
- |> > b[0][1] = 3.0;
- |> > b[1][1] = 4.0;
- |> >end
- |> >
- |> >void Output ( double **b )
- |> >begin
- |> > cout << b[0][0] << "\n";
- |> > cout << b[1][0] << "\n";
- |> > cout << b[0][1] << "\n";
- |> > cout << b[1][1] << "\n";
- |> >end
- |> >
- |> >int main ()
- |> >begin
- |> > double **a = new double*[2];
- |> > a[0] = new double[2];
- |> > a[1] = new double[2];
- |> >
- |> > Input ( a );
- |> > Output ( a );
- |> >
- |> > delete a[0];
- |> > delete a[1];
- |> > delete a;
- |> >
- |> > return 0;
- |> >end
- |>
- |> Yes, I think this program will work always .. since you have passed in
- |> a double ** pointer which is a vector of double*, the least dimension
- |> of the array is not required to correctly index the values.
- |> Although the same syntax can be used to access 2-d arrays (no flames
- |> please .. I mean statically allocated as in double arr[2][2] ) and
- |> variables of type pointer to pointer ( double **), the underlying memory
- |> layout need not be the same. However, the algorithm used to index either
- |> one is the same.. pointer arithmetic.
- |>
- |> I believe( correct me if I am wrong) most or all C/C++ compilers always
- |> use the same pointer arithmetic no matter whether the array passed
- |> is declared as double arr[][least_dim] or double **. This explains
- |> how we can successfully mix pointer arithmetic and array-indexing.
- |> The last dimension is required merely for setting up storage.
- |>
-
- I have always thought (could very well be wrongly so, since I am really
- a "C" programmer, and am just in the process of learning C++ ).
- The pointer arithmetic involves for something like double **a is as
- follows:
- &( a[i][j] ) = a + sizeof( double * ) * i + sizeof( double ) * j
-
- whereas for double a[][least_dim], it will be :
- &( a[i][j] ) = a + sizeof( double ) * ( i * least_dim + j )
-
- I have always declared by multidimensional arrays by the first method
- since it allows me to index arbitraily sized arrays without telling the
- compiler specifically what the dimensions are. I didn't even know you
- can do something like "double arr[][least_dim]" ( that goes to show you
- can still teach an old dog a new trick :) ). Is "least_dim" here a
- run-time variable, like in Fortran, or is it a constant??
-
- Gloria
-