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; } 

output: enter image description here

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

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -