- 4
How many elements are in the following array?
int arr[0][2][4][3];
13 ответов
+ 8
Jay Matthews Indeed... zero based indexes apply when accessing a value from a specific array element.
However, when declaring an array, the numbers between the brackets refer to the size of the array, not position index.
For example, the following line declares an int array with a fixed size of 3 element.
int arr[3]; // Declared with a fixed size of 3.
Below is a list all possible variations for accessing values using zero-based index:
arr[0] = 5; // 1st position at index 0
arr[1] = 10; // 2nd position at index 1
arr[2] = 20; // 3rd position at index 2
+ 6
J.G. Since the type int is specified, it would be a declaration.
+ 5
0 elements - assuming you can declare an array with 0 elements in the 1st dimension, or any dimension for that matter.
0*2*4*3 == 0
If the first dimension is initialized with 0 elements, then the 2nd dimension will never be exist because it's containing element does not exist. This logic subsequently applies to the 3rd and 4th dimensions.
+ 4
David Carroll
Ah! I see now! I didn't catch that before.
+ 3
arr[0] = 1
arr[2] = 3
arr[4] = 5
arr[3] = 4
Ans: 1*3*5*4= 60
+ 2
60
+ 2
Ummm no? In C++ a zero sized array isn't allowed. Even if some compilers allow it the resulting array size is 0.
( sizeof(arr) / sizeof(int) )
Creating a zero sized array through new[] is allowed though but derefencing it is undefined.
+ 2
Dennis
Where did the size 0 array come from? I don't think that is what is being talked about here
+ 2
Dennis
Maybe I'm misinterpreting, but I don't believe that is an array declaration. i think that is supposed to be like an access to the last element of the array. maybe I'm wrong
+ 1
4 elements
0
@J.G.
In the question it says arr[0][2][4][3]?
The 0 makes it illegal.
And even if the 0 was a 1, or if the array was changed to
arr[2][4][3], the size would still be 24, not 60.
0
Pankaj singh
It's a 4D array, not a 1D array initialized to 0, 2, 4, 3.
- 1
index of arr[0]=0
index of arr[1]=2
index of arr[2]=4
index of arr[3]=3