0

我想在 C (Windows) 中复制一个包含空值的字符串。我需要一个函数来传递缓冲区长度,这样 NULL 字符就没有意义了。我找到了 StringCbCopy 函数,但它仍然停在第一个 NULL 字符处。

4

3 回答 3

14

既然您知道长度,请使用memcpy().

于 2011-02-14T17:45:23.747 回答
1

这是一些可能有帮助的快速代码:

char array1[5] = "test", array2[5];
int length = 5;
memcpy(array2, array1, length*sizeof(char));
//the sizeof() is redundant in this because each char is a byte long
//but it is useful if you are working with other datatypes

在这种情况下,memcpy 可能会成为你最好的朋友。

于 2011-02-14T22:05:05.823 回答
0

编写自己的函数来做到这一点应该很容易。如果您知道字符串的长度,只需创建一个指定长度的 char[] 或 char*,并将字符一个一个复制。

于 2011-02-14T17:46:54.870 回答