home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18894 < prev    next >
Encoding:
Text File  |  1993-01-10  |  2.0 KB  |  68 lines

  1. 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
  2. From: jar@solva.ifi.uio.no (Jo Are Rosland)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Passing 2-d arrys to functions
  5. Message-ID: <JAR.93Jan10170817@solva.ifi.uio.no>
  6. Date: 10 Jan 93 16:08:17 GMT
  7. References: <C0Hw4n.Hy9@knot.ccs.queensu.ca>
  8.     <C0H9sA.BGw@newsserver.technet.sg>
  9.     <24568@alice.att.com>
  10.     <726521188snx@trmphrst.demon.co.uk>
  11.     <RUNE.93Jan10125255@pandora.nta.no>
  12. Sender: jar@ifi.uio.no (Jo Are Rosland)
  13. Organization: Dept. of Informatics, University of Oslo, Norway
  14. Lines: 48
  15. Nntp-Posting-Host: solva.ifi.uio.no
  16. In-Reply-To: rune@nta.no's message of 10 Jan 93 12:52:55
  17. X-Md4-Signature: 2b900537518055640010b3c37e968092
  18. Originator: jar@solva.ifi.uio.no
  19.  
  20. In article <RUNE.93Jan10125255@pandora.nta.no> Rune Henning Johansen writes:
  21. > I'm including a little program that seems to work fine. Here I'm pas-
  22. > sing only the pointer to  a two dimensional array.  Can I expect such
  23. > programs to work, and if so, why?
  24.  
  25. No, that's not what you're doing, see below:
  26.  
  27. > #include <stream.h>
  28.  
  29. You should start using <iostream.h>.
  30.  
  31. > #define begin {
  32. > #define end   }
  33.  
  34. Never do this!
  35.  
  36. > void Input ( double **b )
  37.  
  38. Here b is a pointer to a pointer to double.  That's got nothing to do
  39. with arrays.
  40.  
  41. > begin
  42.  
  43. Shudder.
  44.  
  45. > [...]
  46. > int main ()
  47. > begin
  48. >  double **a = new double*[2];
  49. >  a[0] = new double[2];
  50. >  a[1] = new double[2];
  51.  
  52. Here you create two one dimensional double arrays, and a one
  53. dimensional double* array.  Still there are no two-dimensional arrays
  54. in sight.  This could alternatively (and more scaleably) be coded as
  55.  
  56.     double *tmp = new double[4];
  57.     double **a = new double*[2];
  58.     a[0] = tmp;
  59.     a[2] = tmp + 2;
  60.  
  61. Apart from this, the code looks correct.  But aren't you doing far too
  62. much manual work here?  This seems just the right candidate for
  63. encapsulation into a class.
  64.  
  65. --
  66. Jo Are Rosland
  67. jar@ifi.uio.no
  68.