本文介绍了复制C字符串的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来复制C字符串?



我有 const char * stringA 我要 char * stringB 取值(注意 stringB 不是 const )。我尝试了 stringB =(char *)stringA ,但是这使得 stringB 仍然指向相同的内存位置, stringA 后来更改, stringB 也。



've也试过 strcpy(stringB,stringA),但似乎如果 stringB 没有初始化为一个大足够的数组,有一个segfault。我不是超级有经验的C弦,虽然,我错过了明显的东西吗?如果我只是将 stringB 初始化为 char * stringB [23] ,因为我知道我永远不会有一个字符串比 22 个字符(并允许空终止符),是正确的方法吗?如果检查 stringB 与其他C字符串是否相等,额外的空间会影响任何东西吗?



使用字符串不是一个解决方案,因为我需要最小的开销和容易访问各个字符)

解决方案

a href =http://pubs.opengroup.org/onlinepubs/009604599/functions/strdup.html> strdup() 以返回一个C字符串,如:

  #include< string.h> 

const char * stringA =foo;
char * stringB = NULL;

stringB = strdup(stringA);
/ * ... * /
free(stringB);

您也可以使用,但您需要先分配空间,这不难做,但可以导致溢出错误,如果不正确:

  #include< string.h> 

const char * stringA =foo;
char * stringB = NULL;

/ *你必须添加一个以覆盖终止空字符所需的字节* /
stringB =(char *)malloc(strlen(stringA)+ 1);
strcpy(stringB,stringA);
/ * ... * /
free(stringB);



如果不能使用 strdup()我建议使用而不是 strcpy() strncpy()函数复制到 - 并且只能到 - n 字节,这有助于避免溢出错误。如果 strlen(stringA)+ 1> n ,但是,您需要自己终止 stringB 。但是,一般来说,你会知道你需要什么尺寸的东西:

  #include< string.h> 

const char * stringA =foo;
char * stringB = NULL;

/ *你必须添加一个来覆盖终止空字符所需的字节* /
stringB =(char *)malloc(strlen(stringA)+ 1);
strncpy(stringB,stringA,strlen(stringA)+ 1);
/ * ... * /
free(stringB);

我认为 strdup()我自己,所以我试图使用它在处理字符串专有。我不知道是否有POSIX /非POSIX方法的严重缺点,性能方面,但我不是C或C ++专家。



请注意,我把 malloc()的结果转换为 char * 。这是因为您的问题标记为 c ++ 问题。在C ++中,需要转换 malloc()的结果。




b $ b

你去,有一个并发症: strdup()不是在C或C ++。因此,使用具有预大小的数组或 malloc的 strcpy() strncp() 指针。这是一个好习惯,使用 strncp()而不是 strcpy(),无论你可能使用那个函数。这将有助于减少错误的可能性。


Is there an easy way to copy C-strings?

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'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? 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 use strdup() to return a copy of a C-string, as in:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

stringB = strdup(stringA);
/* ... */
free(stringB);    

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);

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);

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.

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

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字符串的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:39