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

  1. Newsgroups: comp.lang.c++
  2. 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
  3. From: suresh@argon.iss.nus.sg (Suresh Thennarangam - Research Scholar)
  4. Subject: Re: Passing 2-d arrys to functions
  5. Message-ID: <1993Jan11.030238.6688@nuscc.nus.sg>
  6. Sender: usenet@nuscc.nus.sg
  7. Reply-To: suresh@iss.nus.sg (Suresh Thennarangam - Research Scholar)
  8. Organization: Institute Of Systems Science, NUS
  9. References: <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk> <RUNE.93Jan10125255@pandora.nta.no>
  10. Date: Mon, 11 Jan 1993 03:02:38 GMT
  11. Lines: 70
  12.  
  13. In article <RUNE.93Jan10125255@pandora.nta.no> rune@nta.no (Rune Henning Johansen FBA) writes:
  14. >In article <726521188snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk (Nikki Locke) writes:
  15. >
  16. >  >  For a two (or more) dimensional array, to access element [m][n], the 
  17. >  >  compiler needs to know the address of the first element, the size of each 
  18. >  >  element, and the number of elements in the least significant dimension. 
  19. >  >  The calculation for an array declared [DIM1][DIM2] is 
  20. >  >  ((m * DIM2 + n) * sizeof(element)).
  21. >
  22. >I'm including a little program that seems to work fine. Here I'm pas-
  23. >sing only the pointer to  a two dimensional array.  Can I expect such
  24. >programs to work, and if so, why?
  25. >
  26. > - Rune
  27. >
  28. > = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  29. >
  30. >#include <stream.h>
  31. >
  32. >#define begin {
  33. >#define end   }
  34. >
  35. >void Input ( double **b )
  36. >begin
  37. >    b[0][0] = 1.0;
  38. >    b[1][0] = 2.0;
  39. >    b[0][1] = 3.0;
  40. >    b[1][1] = 4.0;
  41. >end
  42. >
  43. >void Output ( double **b )
  44. >begin
  45. >    cout << b[0][0] << "\n";
  46. >    cout << b[1][0] << "\n";
  47. >    cout << b[0][1] << "\n";
  48. >    cout << b[1][1] << "\n";
  49. >end
  50. >
  51. >int main ()
  52. >begin
  53. >    double **a = new double*[2];
  54. >    a[0] = new double[2];
  55. >    a[1] = new double[2];
  56. >
  57. >    Input  ( a );
  58. >    Output ( a );
  59. >
  60. >    delete a[0];
  61. >    delete a[1];
  62. >    delete a;
  63. >
  64. >    return 0;
  65. >end
  66.  
  67. Yes, I think this program will work always .. since you have passed in 
  68. a double ** pointer which is a vector of double*, the least dimension
  69. of the array is not required to correctly index the values.
  70. Although the same syntax can be used to access 2-d arrays (no flames
  71. please .. I mean statically allocated as in double arr[2][2] ) and
  72. variables of type pointer to pointer ( double **), the underlying memory 
  73. layout need not be the same. However, the algorithm used to index either
  74. one is the same.. pointer arithmetic. 
  75.  
  76. I believe( correct me if I am wrong) most or all C/C++ compilers always
  77. use the same pointer arithmetic no matter whether the array passed 
  78. is declared as  double arr[][least_dim]  or  double **. This explains
  79. how we can successfully mix pointer arithmetic and array-indexing. 
  80. The last dimension is required merely for setting up storage.
  81.  
  82.  
  83.