No. A multidimensional array is a single block of memory. The size of the block is the product of the dimensions multiplied by the size of the type of the elements, and indexing in each pair of brackets offsets into the array by the product of the dimensions for the remaining dimensions. So..
int arr[5][3][2];
is an array that holds 30 int
s. arr[0][0][0]
gives the first, arr[1][0][0]
gives the seventh (offsets by 3 * 2). arr[0][1][0]
gives the third (offsets by 2).
The pointers the array decays to will depend on the level; arr
decays to a pointer to a 3x2 int array, arr[0]
decays to a pointer to a 2 element int array, and arr[0][0] decays to a pointer to int.
However, you can also have an array of pointers, and treat it as a multidimensional array -- but it requires some extra setup, because you have to set each pointer to its array. Additionally, you lose the information about the sizes of the arrays within the array (sizeof
would give the size of the pointer). On the other hand, you gain the ability to have differently sized sub-arrays and to change where the pointers point, which is useful if they need to be resized or rearranged. An array of pointers like this can be indexed like a multidimensional array, even though it's allocated and arranged differently and sizeof
won't always behave the same way with it. A statically allocated example of this setup would be:
int *arr[3];
int aa[2] = { 10, 11 },
ab[2] = { 12, 13 },
ac[2] = { 14, 15 };
arr[0] = aa;
arr[1] = ab;
arr[2] = ac;
After the above, arr[1][0]
is 12
. But instead of giving the int
found at 1 * 2 * sizeof(int)
bytes past the start address of the array arr
, it gives the int
found at 0 * sizeof(int)
bytes past the address pointed to by arr[1]
. Also, sizeof(arr[0])
is equivalent to sizeof(int *)
instead of sizeof(int) * 2
.