为什么输出的值不会转换为数组

时间:2016-01-22 10:28:16

标签: c++

我正在编写一个程序cpp.sh/9rey

然而,尽管我从第41行的cout函数得到了输出,但是这些值并没有反映在第68行的第68行的数组表中 - > SuccessiveTestArrayInt [i] [1],虽然它对SuccessiveTestArrayInt [i] [0]是正确的。

第41行的cout函数反映的值是否会在稍后重新写入?

int leng = sizeof(ContainsInitialString)/sizeof(ContainsInitialString[0]);

for (int i = 0; i <= leng; i++)
{
    IterationCount=0;
    //if (ChecksRight(i) == 1)
    ChecksRight(i);
        if (SuccessiveCheck == 1)
    {
        cout << "Successive Done" << endl;
        SuccessiveTestArrayChar[RowCount] = { ContainsInitialString[i] };
        cout << "Letters in Success-Char - [" << SuccessiveTestArrayChar[RowCount] << "]" << endl;
        SuccessiveTestArrayInt[RowCount][0] = { IterationCount };
        SuccessiveTestArrayInt[RowCount][1] = { i }; //Position
        m++;
        cout << "Letters in Success-Int repetition, position - [" << SuccessiveTestArrayInt[RowCount][0] << "]["<< SuccessiveTestArrayInt[RowCount][1] << "]  - [ m = " << m << "]" << endl;
        RowCount++;
        i++;
        i = level;

    }
    else
    {
        cout << "not Successful" << endl;
        level++;
        i = level;
    }
}

cout << " ////////////////New part Begins here //////////////////" << endl;
int leng1 = sizeof(SuccessiveTestArrayChar)/sizeof(SuccessiveTestArrayChar[0]);
cout << "leng1 = " << leng1 <<endl; 

for(int i=0;i<=5;i++)
{
    cout << "[" << SuccessiveTestArrayChar[i] << "]" << endl;
}

int leng2 = sizeof(SuccessiveTestArrayInt)/sizeof(SuccessiveTestArrayInt[0]);
cout << " ////////////////SuccessiveTestArrayInt //////////////////" << endl;
for(int i=0;i<=10;i++)
{
    cout << "[" << SuccessiveTestArrayInt[i][0] << "]" <<"[" << SuccessiveTestArrayInt[i][1] << "]" << endl;
}

cout << " ////////////////New part Ends here //////////////////" << endl; 
void ChecksRight(int i)
{
if (ContainsInitialString[i] == ContainsInitialString[i + 1])
{
    level = i;
    cout << "ChecksRight Function; i =" << i << "\ti+1 = " << i+1 <<endl;
    cout << ContainsInitialString[i] << " == " << ContainsInitialString[i + 1] << endl;
    SuccessiveCheck = 1;
    IterationCount++;
    cout << "IterationCount" << IterationCount<<endl;
    ChecksRight(i + 1);
}
else
{
    level++;
}
}

1 个答案:

答案 0 :(得分:1)

您的数组声明如下所示:

int SuccessiveTestArrayInt[50][1];

在第39,68和86行中,您正在访问它:

SuccessiveTestArrayInt[RowCount][1] = { i }; //Position line 39
cout << "[" << SuccessiveTestArrayInt[i][0] << "]" <<"[" << SuccessiveTestArrayInt[i][1] << "]" << endl; // line 68
SuccessiveTestArrayInt[i][1] = SortArrayPosition[d]; // line 86

在上述所有情况下,您正在访问数组越界。由于声明时第二个维度仅为1,因此您只能通过0访问它。任何更大的东西都是越界的。

也许您打算将其声明为:

int SuccessiveTestArrayInt[50][2]; // note the second dimension size