问题描述
是否有一种简单的方法来复制C字符串?
Is there an easy way to copy C strings?
我有 const char * stringA
,我想要 char * stringB
取值(请注意, stringB
不是 const
)。我试过 stringB =(char *)stringA
,但是这使得 stringB
仍然指向相同的内存位置,所以当 stringA
之后会更改, stringB
也会更改。
I have const char *stringA
, and I want char *stringB
to take the value (note that stringB
is not const
). I tried stringB=(char*) stringA
, but that makes stringB
still point to the same memory location, so when stringA
later changes, stringB
does too.
I还尝试了 strcpy(stringB,stringA)
,但似乎如果 stringB
没有初始化为大足够的数组,有一个段错误。我对C字符串不是很有经验,我是否缺少明显的东西?
I've also tried strcpy(stringB,stringA)
, but it seems that if stringB
wasn't initialized to a large enough array, there's a segfault. I'm not super experienced with C strings though, am I missing something obvious?
如果我只是初始化 stringB
作为 char * stringB [23]
,因为我知道我的字符串永远不会超过 22
个字符(并且允许使用空终止符),这是正确的方法吗?如果检查 stringB
与其他C字符串是否相等,多余的空间会影响什么吗?
If I just initialize stringB
as char *stringB[23]
, because I know I'll never have a string longer than 22
characters (and allowing for the null terminator), is that the right way? If stringB
is checked for equality with other C-strings, will the extra space affect anything?
(在这里使用字符串不是解决方案,因为我需要的开销最小并且可以轻松访问单个字符。)
(And just using strings isn't a solution here, as I need minimal overhead and easy access to individual characters.)
推荐答案
您可以使用,但是您需要先分配空间,这并不难但如果执行不正确,则会导致溢出错误:
You could also use strcpy()
, but you need to allocate space first, which isn't hard to do but can lead to an overflow error, if not done correctly:
#include <string.h>
const char *stringA = "foo";
char *stringB = NULL;
/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 );
strcpy( stringB, stringA );
/* ... */
free(stringB);
如果您不能使用 strdup()
,我建议使用而不是 strcpy()
。 strncpy()
函数最多可复制-且最多可复制- n
个字节,这有助于避免溢出错误。如果 strlen(stringA)+ 1> n
,但是,您需要自己终止 stringB
。但是,通常来说,您会知道自己需要什么大小的东西:
If you cannot use strdup()
, I would recommend the use of strncpy()
instead of strcpy()
. The strncpy()
function copies up to — and only up to — n
bytes, which helps avoid overflow errors. If strlen(stringA) + 1 > n
, however, you would need to terminate stringB
, yourself. But, generally, you'll know what sizes you need for things:
#include <string.h>
const char *stringA = "foo";
char *stringB = NULL;
/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 );
strncpy( stringB, stringA, strlen(stringA) + 1 );
/* ... */
free(stringB);
我认为 strdup()
更干净,我自己,所以我尝试在专门用于字符串的地方使用它。在性能方面,我不知道POSIX /非POSIX方法是否存在严重的缺点,但我不是C或C ++专家。
I think strdup()
is cleaner, myself, so I try to use it where working with strings exclusively. I don't know if there are serious downsides to the POSIX/non-POSIX approach, performance-wise, but I am not a C or C++ expert.
请注意,我将 malloc()
的结果转换为 char *
。这是因为您的问题被标记为 c ++
问题。在C ++中,需要从 malloc()
强制转换结果。但是,在C语言中,您不强制转换。
Note that I cast the result of malloc()
to char *
. This is because your question is tagged as a c++
question. In C++, it is required to cast the result from malloc()
. In C, however, you would not cast this.
EDIT
你去了,有一个复杂的问题: strdup()
不在C或C ++中。因此,将 strcpy()
或 strncp()
与预大小数组或 malloc一起使用
-ed指针。在任何可能使用该功能的地方,使用 strncp()
而不是 strcpy()
是一个好习惯。这将有助于减少潜在的错误。
There you go, there's one complication: strdup()
is not in C or C++. So use strcpy()
or strncp()
with a pre-sized array or a malloc
-ed pointer. It's a good habit to use strncp()
instead of strcpy()
, wherever you might use that function. It will help reduce the potential for errors.
这篇关于复制C字符串的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!