When I want to change last element of array,I always use [-1] for last element.
#include <iostream>
using namespace std;
int main(){
int arr[10]{};
arr[0]=10;
arr[-1]=100;
cout<<arr[-1]<<endl;
return 0;
}
Then my teacher say:"C++ doesn't support that kind of behavior with arrays.I should use arr[9] for last elemant and "arr[-1]=100" will actually store 1000 in the area one element before where the array begins. this can cause crash since that value is outside the bounds of the array." Can someone explain why?
Note:I am python programer.When I use -1 in list.I don't have problem in python. Does C++ have different condition ?