home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!sdd.hp.com!think.com!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!aun.uninett.no!nuug!nntp.nta.no!nntp.nta.no!rune
- From: rune@nta.no (Rune Henning Johansen FBA)
- Newsgroups: comp.lang.c++
- Subject: Re: Passing 2-d arrys to functions
- Message-ID: <RUNE.93Jan10125255@pandora.nta.no>
- Date: 10 Jan 93 11:52:55 GMT
- References: <C0Hw4n.Hy9@knot.ccs.queensu.ca> <C0H9sA.BGw@newsserver.technet.sg>
- <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk>
- Sender: news@nntp.nta.no
- Organization: Norwegian Telecom Research
- Lines: 52
- In-Reply-To: nikki@trmphrst.demon.co.uk's message of Fri, 8 Jan 1993 12:26:28 +0000
- Nntp-Posting-Host: pandora.nta.no
-
- 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
-