On Jan 20, 8:01 pm, "Kiran" <Kiran.Ka...@gmail.comwrote:
Hi all,
another newbie question from me, but here goes.
Ok, I have a double array defined as follows:
int cluster[5][5];
now, i know that if you say for example cluster[0], this will give me
the address of where cluster[0][0] resides. now for the program i am
cluster is an array of 5 array of 5 int. clluster[0] is the first of
the 5 arrays. cluster[4] is the last.
Except when the operand of sizeof or &, an expression of type array of
T evaluates to the address of the first element of the array with type
pointer to T. Since cluster is an expression of type array, it will
evaluate to the address of cluster[0] with type pointer to array of 5
int, syntactically int(*)[5]. cluster[4] is also an expression of type
array. It will evaluate to the address of cluster[4][0] with type
pointer to int.
writing, at some point in the program i may want cluster[4] to
reference to cluster[0], meaning that after I do the referencing, if i
say cluster[4][1] = 2, then i should also have cluster[0][1] to be
equal to 2, because now cluster[4] points to the same address as
cluster[0].
Since neither cluster[4] nor cluster[0] are pointers, they area of
memory they refer to cannot be altered.
>
my question is how do i make that reference change?
i tried the following but they gave compile errors or didnt work:
&cluster[4]=cluster[0];
The result of the & operator is not a modifiable l-value and therefore
cannot appear on the left of the assignemtn operator.
cluster[4]=cluster[0];
An array is not a modifiable l-value so see above.
&cluster[4][0]=&cluster[0][0];
thanks in advance for your help!
-- Kiran
You cannot do this with arrays. Ignoring the debate of whether it is a
god idea or not ( vote no), you can do it with pointers.
int icluster[5][5]; /* this is your normal 5x5 array */
int (*pcluster[5])[5] /* an array of five pointers each pointing
to an array of five int */
= {icluster[0], icluster[1], icluster[2], icluster[3],
icluster[4]};
All references to icluster should be done through pcluster, as in
pcluster[4][1] = 2;
At some point, you can assign pcluster[4] the value in pcluster[0] and
every subsequent reference to icluster through pcluster[4] will affect
the elements of icluster[0].