Hi,
I have on large threedimensional array
int largeArray[1024][64][4];
largeArray is an array of 1024 arrays of 64 arrays of 4 int.
In a particular function I only need a part of this array
So I'm using a new variable and assign it the follwoing way:
int (*smallArray)[64][4];
smallArray is a pointer to an array of 64 arrays of 4 int.
smallArray = &largeArray[42];
largeArray[42] is the n-th element (out of the possible 1024 elements)
of largeArray. this element is an array of 64 arrays of 4 int.
&largeArray[42] is the address of this element with the same type as
smallArray.
Is the following code correct if I want to get the value of an element ?
int x = smallArray[0][0];
No. smallArray is a pointer to the first of a series of objects, each
of which is an array of 64 arrays of 4 int. smallArray[0] is the n-th
such object pointed to and is therefore an array of 64 arrays of 4
int. smallArray[0][0] is the n-th array (of the possible 64 arrays)
and has type array of 4 int.
As you have set it up, you need to use (*smallArray)[0][0].
Alternatively, you could consider the following:
largeArray[42] is a particularly object (out of the 1024 that
largeArray consists of) with type 64 arrays of 4 int.
In most contexts, the expression largeArray[42] evaluates to the
address of the first element, that is the address of the first array
of 4 int (out of the possible 64) with type pointer to array of 4 int.
One exception to this rule (there are others) is when the expression
is the operand of the & operators, as it is in your original code.
If you code
int (*smallArray)[4];
you can assign to it with
smallArray = largeArray[42];
and then refer to individual integers with smallArray
[j] which I
find to be a much easier notation to read and to type.
Is there a performance difference between retriveing many elements of the
small and the large array in my function ?
A quality of implementation issue which is not part of the language we
discuss here. You can ask in a newsgroup where your compiler is
discussed.
<<Remove the del for email>>