0

I was trying to add a string to end of a pointer to pointer array, or insert a string in a selected position, then delete a selected element, using 3 function.

The first function I used to add a string to the end of a pointer to pointer:

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

The second function I used to insert a string in a selected position:

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

And here is the function I used to delete any string I choose:

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

I used **add(char **pp, int size, char *str) to add 4 strings to the array, then insert a string in a selected position by **InsPtr(char **pp, int size, int ncell, char *str), or delete a string by **DelPtr(char **pp, int size, int ncell), and it works correctly without any error.

But when I use **InsPtr(char **pp, int size, int ncell, char *str), then after this function I use **DelPtr(char **pp, int size, int ncell) I got an error at runtime.

Here is main function:

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");
}

Here is the result i got before the error at runtime:

1111
2222
3333
4444

Insert in the second index "natasha":

1111
2222
natasha
3333
4444

Delete the second index "natasha":

1111
2222
3333
7
  • Why aren't you just using std::string? Commented Apr 2, 2016 at 17:37
  • 1
    i don't want to use a ready class, just trying to use c. Commented Apr 2, 2016 at 17:43
  • After insert("Natasha"… there is no size++. size should be incremented by insert() then this problem can't arise. Commented Apr 2, 2016 at 17:44
  • 1
    i loss it, and got an error at runtime @ArifBurhan Commented Apr 2, 2016 at 17:50
  • 1
    I tried what you said and got "4444", but also got an error at runtime @ArifBurhan Commented Apr 2, 2016 at 17:51

1 Answer 1

1

Just add size++; after pp = InsPtr(pp, size, insert, "natasha"); and size--; after pp = DelPtr(pp, size, DelS); :)

like this:

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");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.