home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!usc!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!jvnc.net!newsserver.jvnc.net!newsserver.technet.sg!nuscc!argon!suresh
- From: suresh@argon.iss.nus.sg (Suresh Thennarangam - Research Scholar)
- Subject: Re: Passing 2-d arrys to functions
- Message-ID: <1993Jan11.030238.6688@nuscc.nus.sg>
- Sender: usenet@nuscc.nus.sg
- Reply-To: suresh@iss.nus.sg (Suresh Thennarangam - Research Scholar)
- Organization: Institute Of Systems Science, NUS
- References: <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk> <RUNE.93Jan10125255@pandora.nta.no>
- Date: Mon, 11 Jan 1993 03:02:38 GMT
- Lines: 70
-
- 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.
-
-
-