home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18935 < prev    next >
Encoding:
Internet Message Format  |  1993-01-11  |  3.8 KB

  1. Path: sparky!uunet!mcsun!julienas!sophia!xihu.inria.fr!gchow
  2. From: gchow@xihu.inria.fr (Gloria Chow)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Passing 2-d arrys to functions
  5. Message-ID: <38049@sophia.inria.fr>
  6. Date: 11 Jan 93 13:58:08 GMT
  7. References: <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk> <RUNE.93Jan10125255@pandora.nta.no> <1993Jan11.030238.6688@nuscc.nus.sg>
  8. Sender: news@sophia.inria.fr
  9. Organization: INRIA, Sophia-Antipolis (Fr)
  10. Lines: 88
  11.  
  12. In article <1993Jan11.030238.6688@nuscc.nus.sg>, suresh@argon.iss.nus.sg (Suresh Thennarangam - Research Scholar) writes:
  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. I have always thought (could very well be wrongly so, since I am really
  84. a "C" programmer, and am just in the process of learning C++ ).
  85. The pointer arithmetic involves for something like double **a is as
  86. follows:
  87.        &( a[i][j] ) = a + sizeof( double * ) * i + sizeof( double ) * j
  88.  
  89. whereas for double a[][least_dim], it will be :
  90.        &( a[i][j] ) = a + sizeof( double ) * ( i * least_dim + j )
  91.  
  92. I have always declared by multidimensional arrays by the first method
  93. since it allows me to index arbitraily sized arrays without telling the
  94. compiler specifically what the dimensions are.  I didn't even know you
  95. can do something like "double arr[][least_dim]" ( that goes to show you
  96. can still teach an old dog a new trick :) ).  Is "least_dim" here a
  97. run-time variable, like in Fortran, or is it a constant??
  98.  
  99. Gloria
  100.