具有动态分配的 char 数组的 strcpy 函数

发布于 2025-01-11 23:27:04 字数 916 浏览 7 评论 0原文

我面临着使这段代码工作的问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携余温的黄昏 2025-01-18 23:27:04

有几件事我觉得很麻烦。首先,您已经看到人们说您应该使用 std::string 而不是 char 数组,这是事实。但是新程序员应该了解整个语言,因此了解如何使用 char 数组很有价值。

因此,让我们忽略 C++ 字符串并查看您的代码:

char ** test = createCharArray(); // array of pointers to char arrays
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;

让我们从这里开始。我们不知道 createCharArray() 的作用。它是否做了它应该做的一切?它不仅应该创建一个 char 指针数组,而且按照您使用它的方式,它还需要创建它们各自指向的缓冲区。所以它可能看起来像这样:

char ** createCharArray() {
   char ** array = new char *[PARAM_COUNT];
   for (int index = 0; index < PARAM_COUNT; ++index) {
        array[index] = new char[MAXSIZE];
   }
   return array;
}

如果你的看起来非常不同,你可能有问题。

之后,让我们看一下:

for (int i = 0; i < PARAM_COUNT; ++i) {
    strcpy_s(test[i], asd);
}

正如其他人所说,这个版本的 strcpy_s 采用三个参数:
strcpy_s(测试[i], asd, MAXSIZE);

请注意,您多次将同一字符串复制到位。我想知道您的代码是否真的应该这样做:

for (int i = 0; i < PARAM_COUNT; ++i) {
    cin >> asd;
    strcpy_s(test[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:

char ** test = createCharArray(); // array of pointers to char arrays
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;

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:

char ** createCharArray() {
   char ** array = new char *[PARAM_COUNT];
   for (int index = 0; index < PARAM_COUNT; ++index) {
        array[index] = new char[MAXSIZE];
   }
   return array;
}

If yours looks remarkably different, you may have issues.

After that, let's look at this:

for (int i = 0; i < PARAM_COUNT; ++i) {
    strcpy_s(test[i], asd);
}

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:

for (int i = 0; i < PARAM_COUNT; ++i) {
    cin >> asd;
    strcpy_s(test[i], asd, MAXSIZE);
}

And finally, the delete method needs to delete the individual pointers and then the array of pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文