+ 1

Array numbers

suppose I have an array like; int a[4] this. how many elements will it contain, 4 or 5? I think 5 because there will be; a[0], a[1], a[2], a[3], a[4]. But when we're defining, we define it like; int a[4] = {1, 34, 23, 2} so it makes 4 elements. does it only start from a[0] when we define the integer like a[] ? I'm confused about this.

30th Aug 2016, 6:58 PM
Mert Y
Mert Y - avatar
5 Respostas
+ 6
When declaring an array, we input the number of elements starting from 1. So whatever number you put in the square bracket, that is the number of elements the array will have. When it comes to actually using the array, such as printing it out, you start from 0. int a[3] = { 1, 2, 3 }; cout << a[0] << a[1] << a[2]; //output is 123 If we tried to print a[3] in this example, it would throw a runtime error, as it is out of bounds.
30th Aug 2016, 7:10 PM
Cohen Creber
Cohen Creber - avatar
+ 2
You define the number of elements the array will hold when declaring it. So int a[4] will hold 4 elements, and the maximum index of that array will be 3.
30th Aug 2016, 7:16 PM
James Flanders
+ 1
Thank you so much, I understand it clearly now.
30th Aug 2016, 7:14 PM
Mert Y
Mert Y - avatar
+ 1
0 for both
30th Aug 2016, 10:21 PM
Null Void
Null Void - avatar
- 1
yardımcı olalım Türkçe
2nd Sep 2016, 3:16 PM
bayram akdemir
bayram akdemir - avatar