c++ - Simple code of array gives me a Runtime error -
i can not understand why code broke when try run it. use gcc compiler
#include <iostream> using namespace std; int main() { int arr[] = {0}; for(int x=0; x<6; x++) arr[x] = x; for(int y=0; y<6; ++y) cout<< "arr[" << y <<"] = " << arr[y] << endl; return 0; }
int arr[] = {0};
declares array 1 element in it. therefore, valid index array arr[0]
. trying index non-zero integer result in undefined behavior.
Comments
Post a Comment