简体   繁体   English

如何复制 const char* 类型变量的内容?

[英]How to copy contents of the const char* type variable?

I know the difference between:我知道两者之间的区别:

char *a = "string";
char p[] = "string";

from *a , it acts as the following...*a ,它的作用如下...

     +-----+     +---+---+---+---+---+---+---+ 
  a: |  *======> | s | t | r | i | n | g |\0 |    
     +-----+     +---+---+---+---+---+---+---+ 

If I want to create another variable say如果我想创建另一个变量说

char *b;

and I hope it copies all contents in pointer a points to instead of pointing to the a's content.我希望它复制指针 a 指向的所有内容,而不是指向 a 的内容。

     +-----+     +---+---+---+---+---+---+---+      
  a: |  *======> | s | t | r | i | n | g |\0 |    
     +-----+     +---+---+---+---+---+---+---+ 
  b: |  *======> | s | t | r | i | n | g |\0 |    
     +-----+     +---+---+---+---+---+---+---+ 

How to do this ?这该怎么做 ?

In C, you can allocate a new buffer b, and then copy your string there with standard library functions like this:在 C 中,您可以分配一个新的缓冲区 b,然后使用标准库函数将字符串复制到那里,如下所示:

b = malloc((strlen(a) + 1) * sizeof(char));
strcpy(b,a);

Note the +1 in the malloc to make room for the terminating '\\0' .注意malloc+1为终止'\\0'腾出空间。 The sizeof(char) is redundant, but I use it for consistency. sizeof(char)是多余的,但我使用它是为了保持一致性。

In C++, you should use the safer and more elegant std::string :在 C++ 中,你应该使用更安全、更优雅的std::string

std::string b {a};

In C++, you can do在 C++ 中,你可以这样做

const size_t n = strlen(a);   // excludes null terminator
char *b = new char[n + 1]{};  // {} zero initializes array
std::copy_n(a, n, b);

Live Example现场示例

However I recommend using std::string over C-style string since it is但是我建议使用std::string不是 C 风格的字符串,因为它是

  • deals with \\0 -embedded strings correctly正确处理\\0嵌入的字符串
  • safe安全的
  • easy to use便于使用

a 's content, as you posted, points to a read-only memory location set up by the compiler. a的内容,正如您发布的那样,指向编译器设置的只读内存位置。 If you want to have another one at compile-time with distinct values you'll have to define one yourself:如果您想在编译时拥有另一个具有不同值的值,则必须自己定义一个:

char *a = "string";
char *b = "string"; // Nb. This might point to the same location as a

Notice that according to §2.14.5, whether these two pointers will point or not to the same memory location is implementation defined请注意,根据 §2.14.5,这两个指针是否指向同一个内存位置是实现定义的

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation defined.是否所有字符串文字都是不同的(即存储在非重叠对象中)是由实现定义的。

Otherwise go for a heap-stored location like:否则去堆存储的位置,如:

size_t len = strlen(a); // Doesn't include null-terminator character
char *b = new char[len+1];
memcpy(b, a, len); // Assumes plain ASCII string
b[len] = '\0';

I would go for std::string s anyway.无论如何我都会选择std::string

You can use the non-standard (but available on many implementations) strdup function from <string.h> :您可以使用<string.h>的非标准(但在许多实现中可用) strdup函数:

char *b;
strdup(b, a);
...
free(b);

or you can reserve space with malloc and then strcpy :或者您可以使用mallocstrcpy保留空间:

char *b;
b = malloc(strlen(a) + 1);
strcpy(b, a);
...
free(b);

The contents of a is what you have labelled as * in your diagram.的内容a是你已经标记为*您图所示。 a is your little box, and the contents of a are what is in the box! a是你的小盒子, a的内容就是盒子里的东西!

The "string" is NOT the contents of a . “字符串”不是的内容a It's somewhere else in memory, and a contains the address of that string.它在内存中的其他地方, a包含该字符串的地址。

Copying the contents of a to b would end up doing this:复制的内容, ab最终将这样做:

   +-----+     +---+---+---+---+---+---+---+      
a: |  *======> | s | t | r | i | n | g |\0 |    
   +-----+  /  +---+---+---+---+---+---+---+ 
b: |  *====/
   +-----+     

To achieve what you have drawn in your second diagram, you need to take a copy of all the data which a is pointing to.为了实现您在第二个图中绘制的内容,您需要复制a指向的所有数据。 This is not straightforward because how do you decide when to stop copying?这并不简单,因为您如何决定何时停止复制? There's no general way, but if you have predetermined that you just want to copy a string, then you can use a function which copies a string.没有通用的方法,但是如果您已经预先确定只想复制一个字符串,那么您可以使用一个复制字符串的函数。

The common but non-standard strdup function will allocate new space and copy a string.常见但非标准的strdup函数将分配新空间并复制字符串。 Otherwise, you can allocate space (in any of the usual ways of allocating space in C) and then copy the string over to the allocated space.否则,您可以分配空间(以任何在 C 中分配空间的常用方式),然后将字符串复制到分配的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM