+ 1

Output of this code is 8? How

include <iostream> using namespace std; int main() { int arr[] = {1,2,3,4,5,6}; { cout<<arr[6]; } return 0; }

8th Jun 2019, 5:47 PM
Gaurav Pratap singh
Gaurav Pratap singh - avatar
3 Answers
+ 2
thats undefined behavior. When array goes out of bounds, it finds unused memory and prints it out. Your array index begins at 0 and ends at 5.
8th Jun 2019, 6:18 PM
Choe
Choe - avatar
+ 2
The array name is a pointer to the first value it holds. So when you go beyond the memory range of the array, you’re accessing a value you never put in. You may get a larger number if you put in something like 7, and thats probably an address to something else in memory
8th Jun 2019, 6:33 PM
Pete Cowling
Pete Cowling - avatar
+ 1
Thank you both of you
8th Jun 2019, 6:38 PM
Gaurav Pratap singh
Gaurav Pratap singh - avatar