Array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Array

Why have we got some result ? With index that not exist #include <iostream> using namespace std; int main() { int arr[] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 15; x++) { sum += arr[x]; } cout << sum << endl; return 0; }

13th Apr 2018, 12:47 AM
Alberic YAO
Alberic YAO - avatar
1 Answer
+ 14
When using an array literal to initialize an array (providing the data in {}), the array is made the correct size to hold the given data. This array could also be initialized like this: int arr[5] = { 11, 35, 62, 555, 989 }; C++ will not throw an error for using an array index that doesn't exist. Instead, you will retrieve a garbage value, which is why you still receive an answer.
13th Apr 2018, 1:02 AM
Tamra
Tamra - avatar