Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!agate!ronzoni!lippin From: lippin@ronzoni.berkeley.edu (The Apathist) Newsgroups: comp.sys.mac.programmer Subject: Re: THINK C Array Design Flaw Message-ID: <24369@agate.BERKELEY.EDU> Date: 12 May 89 19:05:18 GMT References: <664@loligo.cc.fsu.edu> <24093@agate.BERKELEY.EDU> <696@loligo.cc.fsu.edu> <698@loligo.cc.fsu.edu> <2595@helios.ee.lbl.gov> Sender: usenet@agate.BERKELEY.EDU Reply-To: lippin@math.berkeley.edu Organization: Authorized Service, Incorporated Lines: 41 Recently beard@ux3.lbl.gov (Patrick C Beard) wrote: ] I'm sorry, but I just couldn't let this discussion go by without ] making a comment. The problem you are discussing, passing static or ] automatic arrays, such as foo[40][50] to subroutines with a ] declaration such as foo[][50] is doomed to fail not because of a ] failing in THINK C (formerly LightSpeed C), but because of an inherent ] flaw (feature?) in the C programming language. Namely, the duality ] between arrays and pointers makes passing arrays of dimension >= 2 ] impossible. The reason is that the compiler assumes that a ] declaration of foo[][50] really means *foo[50], an array of pointers. ] The array foo[40][50] is really just one long chunk of memory, and an ] index into it, such as foo[y][x] is really just foo+40*y+x. The ] called subroutine has no knowledge of the inner dimensions of the ] array, and so. has to resort to foo[y][x] meaning *(*(foo+y)+x), or ] *(foo[y]+x). This isn't really a problem: First, a declaration of foo[][50] is treated (in most ways) as (*foo)[50], not *foo[50]. That is, as a pointer to an array rather than as an array of pointers. Second, to index into an array foo[40][50], one calculates &foo[y][x] as &foo[0][0]+50*y+x. The result is that addressing into an array depends on all dimensions but the first, and this is recognized in C syntax, by allowing the first index of a parameter array to be omitted. Note that this has little bearing on (one of) the original complaints, that LSC does not allow formal parameter declarations of the form foo[32][32][32]. While I haven't verified that this is the case, I have found that Sun cc, Gnu cc, and lint all agree with my belief that this is a legal declaration. --Tom Lippincott lippin@math.berkeley.edu "`The problem's all inside your head,' she said to me. `The program's easy if it's done recursively. I'd like to help you in your struggle for a B -- There must be fifty ways to write your program. Fifty ways to write your program.'"