Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site reed.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!vax135!cornell!uw-beaver!tektronix!reed!alexis From: alexis@reed.UUCP (Alexis Dimitriadis) Newsgroups: net.lang.c Subject: Re: how do you dynamically allocate multidimensional arrays? Message-ID: <1401@reed.UUCP> Date: Mon, 29-Apr-85 01:18:12 EDT Article-I.D.: reed.1401 Posted: Mon Apr 29 01:18:12 1985 Date-Received: Tue, 30-Apr-85 06:37:35 EDT References: <669@burl.UUCP> Reply-To: alexis@reed.UUCP (Alexis Dimitriadis) Distribution: net Organization: Reed College, Portland, Oregon Lines: 44 Summary: In article <669@burl.UUCP> geoff@burl.UUCP (geoff) writes: > ... while 'l' correctly packs the data into 8 >contiguous words, 'p' loads the data into every other 2-word segment. If >I increase the column dimension of p to 3, it loads it into every third >2-word segment. >What am I doing wrong?? How do I do it right?? > >int j[100]; /* just getting some initialized space */ >main() >{ > struct fred { > int i; > int j; > }; > struct fred (*p)[2][2]; > struct fred (l)[2][2]; > int i, *ip; > > p = (struct fred (*)[2][2])j; That's easy! The way you declare p, it is a pointer to a two-by-two array of fred. Since p[n] is equivalent to *(p+n), the first index increments by the size of the pointed object--In fact you could use p[x][y][z] without getting a diagnostic from the compiler. The correct way to do this would be to declare struct fred l[2][2], *p[2]; Then you could use p[n], to access the n-1st 8 byte block of memory starting at *p, as in p[n][1].i (and not p[n][1]->i !) Alexis Dimitriadis -- _______________________________________________ As soon as I get a regular job, the opinions expressed above will attach themselves to my employer, who will never be rid of them again. alexis @ reed ...ihnp4!{harvard|tektronix}!reed ...decvax!tektronix!reed ...teneron!reed