从指针指针数组中删除字符串时运行时出错

时间:2016-04-02 17:35:38

标签: c++ arrays

我试图将一个字符串添加到指向指针数组的指针的末尾,或者在选定位置插入一个字符串,然后使用3函数删除所选元素。

我用来在指向指针的末尾添加一个字符串的第一个函数:

        #user_p {
        background-color: red;
        display:inline-block;
        padding-right:100px;
        margin: auto;
    }

        #user_p img {
            display: inline-block;
            width: 200px;
            height: 200px;
        }

    .followings {
        font-family: "Roboto-Light", Helvetica, Arial, sans-serif;
        width: 200px;
        display: inline-block;
        background-color: green;
        margin-left: 60px;
        text-align: center;
    }

我用于在选定位置插入字符串的第二个函数:

char **add(char **pp, int size, char *str)
{
    if (size == 0)
    {
        pp = new char *[size+1];
    }
    else
    {
        char **temp = new char *[size+1];
        for (int i = 0; i < size; i++)
        {
            temp[i] = pp[i];
        }
        delete[]pp;
        pp = temp; 
    }
    pp[size] = new char[strlen(str) + 1];
    strcpy(pp[size], str);
    return pp;
}

这是我用来删除我选择的任何字符串的函数:

char **InsPtr(char **pp, int size, int ncell, char *str)
{
    char**temp = new char *[size+1];  //добавить новый элемент [size+1]
    for (int i = 0, j = 0; j < size + 1; j++)
    {
        if (j != ncell)
        {
            temp[j] = pp[i];
            i++;
        }
        else
        {
            temp[j]=new char[ strlen(str)+1];
            strcpy(temp[j], str);
        }
    }
    delete[] pp;
    pp = temp;
    return pp;
}

我使用char **DelPtr(char **pp, int size, int ncell) { char**temp = new char*[size-1]; for (int i = 0, j = 0; i < size; i++) { if (i != ncell) { temp[j] = pp[i]; j++; } } delete[]pp[ncell]; pp[ncell] = 0; delete[] pp; pp = temp; return pp; } 向数组中添加4个字符串,然后按**add(char **pp, int size, char *str)将字符串插入选定位置,或者按**InsPtr(char **pp, int size, int ncell, char *str)删除字符串,并且无需任何字符串正常工作错误。

但是当我使用**DelPtr(char **pp, int size, int ncell)时,在此功能之后我使用**InsPtr(char **pp, int size, int ncell, char *str) 我在运行时遇到错误。

这是主要功能:

**DelPtr(char **pp, int size, int ncell)

以下是我在运行时错误之前得到的结果:

void main()
{   
    int size = 0; 
    char **pp = 0;
    pp = add(pp, size, "1111");
    size++;
    pp = add(pp, size, "2222");
    size++;
    pp = add(pp, size, "3333");
    size++;
    pp = add(pp, size, "4444");
    size++;
    int insert=2,DelS= 2;
    for (int i = 0; i < size; i++)
    {
        cout << *(pp + i) << endl;
    }
    pp = InsPtr(pp, size, insert, "natasha");
    cout << endl;

    for (int i = 0; i < size + 1; i++)
    {
        cout << *(pp + i) << endl;
    }
    pp = DelPtr(pp, size, DelS);
    cout << endl;
    for (int i = 0; i < size ; i++)
    {
        cout << *(pp + i) << endl;
    }
    system("pause");
}

插入第二个索引“natasha”:

1111
2222
3333
4444

删除第二个索引“natasha”:

1111
2222
natasha
3333
4444

1 个答案:

答案 0 :(得分:1)

size++;之后添加pp = InsPtr(pp, size, insert, "natasha");size--;之后的pp = DelPtr(pp, size, DelS);:)

像这样:

void main()
{   
    int size = 0; 
    char **pp = 0;
    pp = add(pp, size, "1111");
    size++;
    pp = add(pp, size, "2222");
    size++;
    pp = add(pp, size, "3333");
    size++;
    pp = add(pp, size, "4444");
    size++;
    int insert=2,DelS= 2;
    for (int i = 0; i < size; i++)
    {
    cout << *(pp + i) << endl;
    }
     pp = InsPtr(pp, size, insert, "natasha");
     cout << endl;
    for (int i = 0; i < size + 1; i++)
    {
    cout << *(pp + i) << endl;
    }
    size++;
    pp = DelPtr(pp, size, DelS);
    cout << endl;
    size--;
    for (int i = 0; i < size ; i++)
    {
        cout << *(pp + i) << endl;
    }
    system("pause");
}