home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!utcsri!torn!spool.mu.edu!howland.reston.ans.net!paladin.american.edu!gatech!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
- From: jar@solva.ifi.uio.no (Jo Are Rosland)
- Newsgroups: comp.lang.c++
- Subject: Re: Passing 2-d arrys to functions
- Message-ID: <JAR.93Jan10170817@solva.ifi.uio.no>
- Date: 10 Jan 93 16:08:17 GMT
- References: <C0Hw4n.Hy9@knot.ccs.queensu.ca>
- <C0H9sA.BGw@newsserver.technet.sg>
- <24568@alice.att.com>
- <726521188snx@trmphrst.demon.co.uk>
- <RUNE.93Jan10125255@pandora.nta.no>
- Sender: jar@ifi.uio.no (Jo Are Rosland)
- Organization: Dept. of Informatics, University of Oslo, Norway
- Lines: 48
- Nntp-Posting-Host: solva.ifi.uio.no
- In-Reply-To: rune@nta.no's message of 10 Jan 93 12:52:55
- X-Md4-Signature: 2b900537518055640010b3c37e968092
- Originator: jar@solva.ifi.uio.no
-
- In article <RUNE.93Jan10125255@pandora.nta.no> Rune Henning Johansen writes:
- > 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?
-
- No, that's not what you're doing, see below:
-
- > #include <stream.h>
-
- You should start using <iostream.h>.
-
- > #define begin {
- > #define end }
-
- Never do this!
-
- > void Input ( double **b )
-
- Here b is a pointer to a pointer to double. That's got nothing to do
- with arrays.
-
- > begin
-
- Shudder.
-
- > [...]
- > int main ()
- > begin
- > double **a = new double*[2];
- > a[0] = new double[2];
- > a[1] = new double[2];
-
- Here you create two one dimensional double arrays, and a one
- dimensional double* array. Still there are no two-dimensional arrays
- in sight. This could alternatively (and more scaleably) be coded as
-
- double *tmp = new double[4];
- double **a = new double*[2];
- a[0] = tmp;
- a[2] = tmp + 2;
-
- Apart from this, the code looks correct. But aren't you doing far too
- much manual work here? This seems just the right candidate for
- encapsulation into a class.
-
- --
- Jo Are Rosland
- jar@ifi.uio.no
-