multi-d arrays and types
Karl Heuer
karl at haddock.ima.isc.com
Tue Mar 27 05:55:47 AEST 1990
In article <1990Mar26.155319.23986 at ccu.umanitoba.ca> rpjday at 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 at ima.ima.isc.com or harvard!ima!karl), The Walking Lint
More information about the Comp.lang.c
mailing list