dynamic array parameters
Wayne Throop
throopw at dg_rtp.UUCP
Fri Dec 27 04:36:47 AEST 1985
To reference an element of a variably sized multi-dimensional array,
David Tribble recommends
> (*(a + i*d1 + j)) /* same as a[i][j] */
along with using macros to simplify, like so:
> #define index(a,i,j) (*(a + i*d1 + j))
> index(a, i, j) /* same as a[i][j] */
Good idea. I'd just like to note that, using pointer/array equivalence,
the somewhat complicated expression can be written a little less
confusingly, as
a[i*d1 + j]
It might still be advisable to use the macro, but then again, maybe not.
The more-than-two-dimensional cases are still pretty confusing:
a[(i*d1 + j)*d2 + k]
Also, I'd recommend declaring the formal "a" as "int a[]" rather than
"int *a", unless it is important for some reason to treat "a" as an
lvalue.
--
Wayne Throop at Data General, RTP, NC
<the-known-world>!mcnc!rti-sel!dg_rtp!throopw
More information about the Comp.lang.c
mailing list