尝试在数组中添加元素时,堆栈变量已损坏

时间:2014-09-15 02:10:35

标签: c arrays

我正在尝试将元素添加到2d数组中。但它说

  

运行时检查失败#2 - 变量'pos'周围的堆栈已损坏。

这是我的代码

void addTheNewTask(char(*array)[SIZE_OF_ARRAY], int* Row)
{
    int row = *Row;
    if (row == SIZE_OF_ARRAY)
    {
        printf("To DO List ...already full....please remove some items and create space to add more");
        return;
    }
    int pos;
    printf("Please enter the index where you want to Enter the new task\n");
    scanf("%d", &pos);
    char str1[SIZE_OF_ARRAY];
    printf("\nPlease Enter the task\n");
    scanf("\n%[^\n]", str1);
    if (pos >= SIZE_OF_ROW)
    {
        printf("Crossing the maximum array size");
        return;
    }
    if (pos != row + 1 && pos>row)
    {
        printf("There are empty slots before....try to insert their first.");
        return;
    }
    if (pos == row + 1)
    {
        strcpy(array[pos], str1);
    }
    else
    {
        //now we have to store the string already at index pos
        char str2[SIZE_OF_ARRAY];
        strcpy(str2, array[pos]);
        strcpy(array[pos], str1);
        int i;
        for (i = pos + 1; i<row; i++)
        {
            strcpy(str1, array[i]);
            strcpy(array[i], str2);
            strcpy(str2, str1);
        }
        strcpy(array[i], str2);
        *Row = row + 1;

    }


    return;
}

在忽略几次之后,它就像我想要的那样工作,但我只是想知道为什么会发生这种情况?我能解决吗?

谢谢!

0 个答案:

没有答案