Xref: utzoo comp.lang.fortran:757 comp.lang.c:10716 Path: utzoo!attcan!uunet!husc6!spdcc!ima!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.fortran,comp.lang.c Subject: Dynamic multidimensional arrays Message-ID: <4556@haddock.ISC.COM> Date: 14 Jun 88 23:09:28 GMT References: <10655@agate.BERKELEY.EDU> <1857@hubcap.UUCP> <10023@ames.arc.nasa.gov> <10032@ames.arc.nasa.gov> <59@cubsun.BIO.COLUMBIA.EDU> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Followup-To: comp.lang.c Organization: Interactive Systems, Boston Lines: 27 In article <59@cubsun.BIO.COLUMBIA.EDU> shenkin@cubsun.UUCP (Peter Shenkin) writes: >[What about] > float **matrix( int nrl, int nrh, int ncl, int nch ) >[and its N-dimensional generalization?] > float **array( int Ndim, > int n1l, int n1h, int n2l, int n2h, ..., int nNl, int nNh ) Since normal arrays in C are zero-based, I don't see any reason to specify the lower bounds. If you allocate the entire array (including space for the pointers as well as the contents) in a single chunk, then you don't need all those free_array() routines -- the standard free() will work. I've written it this way. The N-dimensional generalization is incorrectly typed. Since the number of levels of indirection is not known at compile-time, it should be "void *". There's a problem with the lack of generality. You suggest implementing these in triplicate: arrays of integer, float, or double are allowed. What if some other type is desired, e.g. long int, or some random struct? I'd prefer a syntax that expects the type-name as an argument to the macro: float **mtrx = array2d(float, nr, nc); Unfortunately, on some architectures the only way to implement this correctly requires a (minor) hook in the compiler. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint Followups to comp.lang.c only.