home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!littlei!hfglobe!chnews!sedona!bhoughto
- From: bhoughto@sedona.intel.com (Blair P. Houghton)
- Newsgroups: comp.std.c
- Subject: Re: Allocating memory like an array
- Message-ID: <1debc4INNieg@chnews.intel.com>
- Date: 6 Nov 92 17:52:04 GMT
- References: <1992Nov5.203939.1323@fcom.cc.utah.edu>
- Organization: Intel Corp., Chandler, Arizona
- Lines: 39
- NNTP-Posting-Host: alfalfa.intel.com
-
- In article <1992Nov5.203939.1323@fcom.cc.utah.edu> tim@osiris.usi.utah.edu (Tim Burns 581-4439) writes:
- >memory dynamically. Right now, when I allocate my array using the
- >standard array allocation: double A[L][M], where L and M are constants,
-
- What you see is that `A' has a type compatible with `(double *)',
- whereas what the _Numerical_Recipes_ method does is create
- `m' as compatible with `(double **)'.
-
- All you need to do is allocate enough space for the
- two-dimensional array. C's array-memory mapping is flat,
- in row-major order.
-
- You want L rows of M columns of doubles, so you need
-
- m = (double *) malloc ( L * M * sizeof(double) );
-
- and now `m' is compatible with `A'.
-
- Accesses to `m' can now be made using C's array-access syntax;
- That is,
-
- int row, col;
- ...
-
- m[row][col]
-
- gives the number at row and col.
-
- m[row][col+1]
-
- gives the next number in `m', and
-
- m[row+1][col]
-
- gives the number in the next row, i.e., L elements away from
- the number in this row of `m'.
-
- --Blair
- "Isn't this just a little basic?"
-