Reputation: 344
I was kind of a Java-holic. I wanted to learn more about arrays in C. Is there such an array:
int test[5][5][5]
I want to know how many integer elements it has. I had three questions:
int
type elements. Is that correct?test
has 125 *int
type elements?test
has 5 **int
type elements?Upvotes: 2
Views: 240
Reputation: 43662
Only the first statement is correct. And you also have another guarantee: multi-dimensional arrays in C++ (notice that C and C++ are two different languages) allocate contiguous memory blocks
[dcl.array] An object of array type contains a contiguously allocated non-empty set of N subobjects of type T.
so the following holds:
int array[5][5][5];
int array2[125];
std::cout << (sizeof(array) == sizeof(array2)); // Yes
Indices are an abstraction over a different access-pattern.
The second is not correct (int*
is a different type than int
). And even in memory-space terms it is generally not guaranteed that an array of 125 integer elements will occupy the same space as an array of 125 integer pointers.
Ditto for the third one.
As a sidenote: arrays are often manipulated through pointers and there's an entire topic called "pointers arithmetic" which goes hand-in-hand with arrays, not to mention arrays decaying into pointers but arrays are not pointers.
Upvotes: 0
Reputation: 311048
Let consider declaration
int test[5][5][5];
step by step.
At first consider declaration like
int test[5];
test has 5 elements of type int
.
Now consider
int test[5][5];
It declares an array of 5 elements of the preceding type int[5]
. So as each element of the array in turn an array and contains 5 elements then the total number of elements of type int contained in test is equal to 5 * 5 that is 25.
Now let's return to declaration
int test[5][5][5];
It defines an array elements of which has type int[5][5]
. Each element contains 25 objects of type int
. The array has 5 such elements. So in total it contains 5 * 25 objects of type int
that is 125 such objects.
So array
int test[5][5][5];
has 5 elements of type int[5][5]
array
int test[5][5];
has 5 elements of type int[5]
and array
int test[5];
has 5 elements of type int
.
In C++ a multidimensional array is an array elements of which are in turn arrays (not pointers).
Upvotes: 0
Reputation: 110698
firstly, i think there are 125 int type elements.
That's right. It's an array of 5 arrays of 5 arrays of 5 int
s, which means there's a total of 125 int
s.
secondly, is it correct that above 'test' array has 125 *int type elements?
*int
is not a valid type - presumably you mean int*
. Either way, the answer is no. There are no pointers here.
thirdly, is it correct that the 'test' array has 5 **int type elements?
Again, **int
is probably meant to be int**
, but the answer is no. No pointers in this declaration.
Presumably you're asking this because you have some idea that "arrays are pointers" or "arrays are just pointers under the hood" - this idea is wrong. When you ask for an int[5][5][5]
, you get exactly that - a 3-dimensional array consisting of a total of 125 int
objects.
Upvotes: 7
Reputation: 29724
This array has 125 elements of type int
, it doesn't contain anything besides these integers.
Upvotes: 0