#include <iostream>
using namespace std;
int main()
{
int arr1[4];
int arr2[4];
for (int i = 0;i<=4;i++)
{
cin>>arr1[i];
arr2[i]=arr1[i];
}
for(int j = 0;j<=4;j++)
{
cout<<arr1[j]<<" ";
cout<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Outcome:
Can someone explain to me why arr[0] = 5
? I cant figure it out.
You accessed out of bounds when i=4
. arr1
and arr2
have only 4 elements. ie arr1[0], arr1[1], arr1[2], arr1[3]
and arr2[0], arr2[1], arr2[2], arr2[3]
.
Your compiler may assigned arr1
just after arr2
, and accidentaly arr2 + 4
had the same address as arr1
, so the access to arr2[4]
wrote the value to arr1[0]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.