+ 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.
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.
+ 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.
+ 1
Thank you so much, I understand it clearly now.
+ 1
0 for both
- 1
yardımcı olalım Türkçe