home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / c / 2977 < prev    next >
Encoding:
Internet Message Format  |  1992-11-07  |  1.4 KB

  1. Path: sparky!uunet!littlei!hfglobe!chnews!sedona!bhoughto
  2. From: bhoughto@sedona.intel.com (Blair P. Houghton)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Allocating memory like an array
  5. Message-ID: <1debc4INNieg@chnews.intel.com>
  6. Date: 6 Nov 92 17:52:04 GMT
  7. References: <1992Nov5.203939.1323@fcom.cc.utah.edu>
  8. Organization: Intel Corp., Chandler, Arizona
  9. Lines: 39
  10. NNTP-Posting-Host: alfalfa.intel.com
  11.  
  12. In article <1992Nov5.203939.1323@fcom.cc.utah.edu> tim@osiris.usi.utah.edu (Tim Burns 581-4439) writes:
  13. >memory dynamically.  Right now, when I allocate my array using the 
  14. >standard array allocation:  double A[L][M], where L and M are constants,
  15.  
  16. What you see is that `A' has a type compatible with `(double *)',
  17. whereas what the _Numerical_Recipes_ method does is create
  18. `m' as compatible with `(double **)'.
  19.  
  20. All you need to do is allocate enough space for the
  21. two-dimensional array.  C's array-memory mapping is flat,
  22. in row-major order.
  23.  
  24. You want L rows of M columns of doubles, so you need
  25.  
  26.     m = (double *) malloc ( L * M * sizeof(double) );
  27.  
  28. and now `m' is compatible with `A'.
  29.  
  30. Accesses to `m' can now be made using C's array-access syntax;
  31. That is,
  32.  
  33.     int row, col;
  34.     ...
  35.  
  36.     m[row][col]
  37.  
  38. gives the number at row and col.
  39.  
  40.     m[row][col+1]
  41.  
  42. gives the next number in `m', and
  43.  
  44.         m[row+1][col]
  45.  
  46. gives the number in the next row, i.e., L elements away from
  47. the number in this row of `m'.
  48.  
  49.                 --Blair
  50.                   "Isn't this just a little basic?"
  51.