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

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -