所以我试图将我的原始数组分成两部分,并且工作正常,给我输出结果。但是问题出在输出
之后运行时检查失败#2-变量'array2'周围的堆栈已损坏。
这是代码,请帮忙!
#include<iostream>
using namespace std;
int main()
{
// My array
int number[8] = { 7,1,5,3,6,8,4,2 };
// length/size of my array
const int length = sizeof(number) / sizeof(*number);
// temporary array1
int array1[length / 2];
// temporary array1 length/size
int arrLength1 = sizeof(array1) / sizeof(*array1);
// initializing the temporary array1.
for (int i = 0; i < arrLength1; i++) {
array1[i] = number[i];
}
// printing the array1
for (int i = 0; i < arrLength1; i++) {
cout << array1[i] << ", ";
}
cout << endl;
// temporary array2
int array2[length / 2];
// temporary array2 length/size
int arrLength2 = sizeof(array2) / sizeof(*array2);
// initializing the temporary array2.
for (int i = arrLength1; i < length; i++) {
array2[i] = number[i];
}
// printing array2
for (int i = arrLength1; i < length; i++) {
cout << array2[i] << ", ";
}
cout << endl;
system("pause");
return 0;
}
答案 0 :(得分:1)
您的array2并未初始化其索引中从0到arrLength的任何内容。尝试更改此行
for (int i = arrLength1; i < length; i++)
到
for (int i = 0; i < length / 2; i++)