Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site utah-cs.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!pesnta!hplabs!utah-cs!donn From: donn@utah-cs.UUCP (Donn Seeley) Newsgroups: net.lang.c Subject: Re: help converting multi-dim arrays Message-ID: <3286@utah-cs.UUCP> Date: Sun, 14-Apr-85 02:19:43 EST Article-I.D.: utah-cs.3286 Posted: Sun Apr 14 02:19:43 1985 Date-Received: Mon, 15-Apr-85 05:51:38 EST Organization: University of Utah CS Dept Lines: 34 It's not strictly necessary to have an array of pointers to provide a multi-dimensional array that varies in size, provided the variation is limited to one dimension. The following example shows one way to dynamically allocate a multi-dimensional array: ------------------------------------------------------------------------ # define MAXROWS 10000 # define MAXCOLS 1000 f( nrows ) int nrows; { register double (*dp)[MAXROWS][MAXCOLS]; char *malloc(); dp = (double (*)[MAXROWS][MAXCOLS]) malloc( nrows * MAXCOLS * sizeof (double) ); if ( dp == NULL ) bomb( "Out of memory" ); (*dp)[1][0] = 1.0; ... free( (char *) dp ); return; } ------------------------------------------------------------------------ I believe this is portable code, going on the basis of section 14.3 of the C reference manual, but I'd be happy to be corrected if wrong. (Actually, the main reason I like this example is that it provides a rare instance of a use for one of the more bizarre C type declarations...) Donn Seeley University of Utah CS Dept donn@utah-cs.arpa 40 46' 6"N 111 50' 34"W (801) 581-5668 decvax!utah-cs!donn