在运行时从指针到指针数组中删除字符串时出错
Error at runtime while deleting a string from a pointer to pointer array
我试图使用3函数在指针到指针数组的末尾添加一个字符串,或者在选定的位置插入字符串,然后删除选定的元素。
我使用的第一个函数是在指向指针的指针末尾添加字符串:
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;
}
我用**add(char **pp, int size, char *str)
向数组中添加了4个字符串,然后用**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");
}
以下是我在运行时出现错误之前得到的结果:
1111
2222
3333
4444
在第二个索引"natasha"中插入:
1111
2222
natasha
3333
4444
删除第二个索引"natasha":
1111
2222
3333
只需在pp = InsPtr(pp, size, insert, "natasha");
之后添加size++;
和pp = DelPtr(pp, size, DelS);
之后的size--;
:)
像这样:
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");
}
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 使用指针初始化 char 数组或字符串 C++
- 指针保留字符串
- Visual c ++,使用字符串引用/指针调用 dll 函数
- strcpy 在类中无法使用字符串指针
- C++为 void 指针分配一个字符串
- OpenGL 统一名称指针作为字符串
- 如何将字符串指针数组转换为字符串类型的智能指针向量?
- 转换指针引用的字符串
- 未分配释放C++(C 字符串)指针
- 为什么 C 样式字符串的工作空指针检查不?
- 为什么多维数组中的空字符串文本衰减为空指针?
- 使用终止程序的指针在数组中输入字符串
- 将指针作为缓冲区传递到第一个字符串元素
- 指针 - 字符串C
- C++:更改指针字符串的值
- 使用带字符指针字符串的基于范围的For循环
- 转换/解析指针字符串和双精度
- 有问题分配字符串指针字符串值