本文介绍了能向我解释的人本C ++数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

结果:

有人能向我解释为什么改编[0] = 5 ?我不能看着办吧。

Can someone explain to me why arr[0] = 5? I cant figure it out.

推荐答案

您访问越界时 I = 4 的。 ARR1 ARR2 只有4个元素。即 ARR1 [0],ARR1 [1],ARR1 [2],ARR1 [3] ARR2 [0],ARR2 [1] ARR2 [2],ARR2 [3]

You accessed out of bounds when i=4. arr1 and arr2 have only 4 elements. i.e. arr1[0], arr1[1], arr1[2], arr1[3] and arr2[0], arr2[1], arr2[2], arr2[3].

您的编译器可分配 ARR1 就在 ARR2 和不期而遇 ARR2 + 4 有相同的地址 ARR1 ,所以获得 ARR2 [4] 写的值到 ARR1 [0]

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].

这篇关于能向我解释的人本C ++数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:51