Path: utzoo!attcan!uunet!cs.utexas.edu!yale!think!snorkelwacker!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: multi-d arrays and types Message-ID: <16289@haddock.ima.isc.com> Date: 26 Mar 90 19:55:47 GMT References: <1990Mar26.155319.23986@ccu.umanitoba.ca> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 28 In article <1990Mar26.155319.23986@ccu.umanitoba.ca> rpjday@ccu.umanitoba.ca writes: > int calendar[12][31]; > int (*monthptr)[31]; > for (monthptr = calendar; monthptr <= calendar[11]; monthptr++) ... >[Assignment and increment are okay, but the comparison has a problem.] Since "calendar" has type "array of array of int", "calendar[11]" has type "array of int", which decays into "pointer to int", which does not match the "pointer to array of int" on the left side. What you really want is "&calendar[11]", but... >Putting an "&" in front of "calendar[11]" makes it even worse as the compiler >complains that I now have "& before array or function", tosses the "&", then >generates the first warning. Unfortunately, in pre-ANSI C it was not legal to explicitly take the address of an array. (This was a botch; the relationship between arrays and pointers was not as well defined in the old days.) Your options include: [0] use an ANSI C compiler; [1] patch your compiler to remove (nothing to add!) the line that makes this illegal; [2] rewrite your code with a dummy struct around the array; [3] (recommended) change the expression from "&calendar[11]" to "calendar+11", which is equivalent (due to the definition of "[]") but works right even in pre-ANSI C. Oh, I also recommend that you use 12 rather than 11 as the limiting subscript, and change the relational operator to a strict "<". Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint