具有动态分配的 char 数组的 strcpy 函数
我面临着使这段代码工作的问题:
char **createCharArray() {
char **charArray = new char*[PARAM_COUNT];
for (int i = 0; i < PARAM_COUNT; ++i) {
charArray[i] = new char[MAXSIZE];
}
return charArray;
}
void deleteCharArray(char **charArray) {
for (int i = 0; i < PARAM_COUNT; ++i) {
delete[] charArray[i];
}
delete[] charArray;
}
int main(){
char ** test = createCharArray();
char *asd = new char[MAXSIZE];
cin >> asd;
for (int i = 0; i < PARAM_COUNT; ++i) {
strcpy_s(test[i], asd);
}
for (int i = 0; i < PARAM_COUNT; ++i) {
cout << i << " " << test[i] << endl;
}
deleteCharArray(test);
return 0;
}
如何将该字符串复制到 char 数组中,我在哪里弄错了?
编辑:正如评论中的 Igor Tandetnik 和 user17732522 以及下面的回复中 Joseph Larson 的回答,这是通过向 strcpy_s 函数添加 buffer 参数来解决的,使其总共有 3 个参数。
I am facing problems to make this piece of code work:
char **createCharArray() {
char **charArray = new char*[PARAM_COUNT];
for (int i = 0; i < PARAM_COUNT; ++i) {
charArray[i] = new char[MAXSIZE];
}
return charArray;
}
void deleteCharArray(char **charArray) {
for (int i = 0; i < PARAM_COUNT; ++i) {
delete[] charArray[i];
}
delete[] charArray;
}
int main(){
char ** test = createCharArray();
char *asd = new char[MAXSIZE];
cin >> asd;
for (int i = 0; i < PARAM_COUNT; ++i) {
strcpy_s(test[i], asd);
}
for (int i = 0; i < PARAM_COUNT; ++i) {
cout << i << " " << test[i] << endl;
}
deleteCharArray(test);
return 0;
}
How do I copy that string into the char array, where am I mistaking?
Edit: As answered by Igor Tandetnik and user17732522 in the comments and Joseph Larson in the reply below, this was solved by adding the buffer argument to the strcpy_s function, making it a total of 3 arguments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几件事我觉得很麻烦。首先,您已经看到人们说您应该使用 std::string 而不是 char 数组,这是事实。但是新程序员应该了解整个语言,因此了解如何使用 char 数组很有价值。
因此,让我们忽略 C++ 字符串并查看您的代码:
让我们从这里开始。我们不知道
createCharArray()
的作用。它是否做了它应该做的一切?它不仅应该创建一个 char 指针数组,而且按照您使用它的方式,它还需要创建它们各自指向的缓冲区。所以它可能看起来像这样:如果你的看起来非常不同,你可能有问题。
之后,让我们看一下:
正如其他人所说,这个版本的
strcpy_s
采用三个参数:strcpy_s(测试[i], asd, MAXSIZE);
请注意,您多次将同一字符串复制到位。我想知道您的代码是否真的应该这样做:
最后,删除方法需要删除各个指针,然后删除指针数组。
There are a few things I find troublesome. First, you've seen people say you should use std::string instead of char arrays, and that's true. But new programmers should understand the entire language, and so understanding how to use char arrays has value.
So let's ignore C++ strings and look at your code:
Let's start with this. We don't know what
createCharArray()
does. Is it doing everything it should? Not only should it create an array of char pointers, but the way you're using it, it also needs to create the buffers they each point to. So it might look something like this:If yours looks remarkably different, you may have issues.
After that, let's look at this:
As others have said, this version of
strcpy_s
takes three arguments:strcpy_s(test[i], asd, MAXSIZE);
Note that you're copying the same string into place multiple times. I wonder if your code should really do this:
And finally, the delete method needs to delete the individual pointers and then the array of pointers.