How come I can access non-existent array values and not get any errors??? It just prints 0 when the index is out of bounds, as if the value of that index is 0. Please explain.
#include <iostream>
using namespace std;
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int main()
{
for (int i = 0; i < 10; i++)
{
cout << arr << endl << endl; // this works as expected
}
cout << arr[10] << endl // this prints 0
<< arr[11] << endl // this prints 0
<< arr[22]; // this prints 0 as well. No errors, nothing.
return 0;
}
Using Code::Blocks.