C语言 将 C 中的字符串文字复制到字符数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25838628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:22:53  来源:igfitidea点击:

Copying string literals in C into an character array

cstring

提问by Anvita Potluri

I have a string literal:

我有一个字符串文字:

char *tmp = "xxxx";

I want to copy the string literal into an array.

我想将字符串文字复制到数组中。

For example:

例如:

  • how do I copy tmpinto an char array[50]?

  • and how to copy one string literal to another?

  • 我如何复制tmp到一个char array[50]

  • 以及如何将一个字符串文字复制到另一个?

回答by Pavel ?imerda

Use strcpy(), strncpy(), strlcpy()or memcpy(), according to your specific needs.

使用strcpy()strncpy()strlcpy()或者memcpy(),根据您的具体需求。

With the following variables:

使用以下变量:

const char *tmp = "xxxx";
char buffer[50];

You typically need to ensure your string will be null terminated after copying:

您通常需要确保您的字符串在复制后以空字符结尾:

memset(buffer, 0, sizeof buffer);
strncpy(buffer, tmp, sizeof buffer - 1);

An alternative approach:

另一种方法:

strncpy(buffer, tmp, sizeof buffer);
buffer[sizeof buffer - 1] = '
strlcpy(buffer, tmp, sizeof buffer);
';

Some systems also provide strlcpy()which handles the NUL byte correctly:

一些系统还提供strlcpy()了正确处理 NUL 字节的方法:

size_t strlcpy(char *dest, const char *src, size_t n)
{
    size_t len = strlen(src);

    if (len < n)
        memcpy(dest, src, len + 1);
    else {
        memcpy(dest, src, n - 1);
        dest[n - 1] = '
strcpy(buffer, tmp);
'; } return len; }

You could naively implement strlcpy()yourself as follows:

你可以天真地实现strlcpy()自己如下:

const char* tmp = "xxxx";
// ...
char array[50];
// ...
strcpy(array, tmp);

The above code also serves as an example for memcpy().

上面的代码也作为memcpy().

Finally, when you already know that the string will fit:

最后,当您已经知道字符串适合时:

const char* tmp = "xxxx";
char array[50];
// ...
array[49] = '
// For Microsoft Visual C++:

  char dst[40];

  strcpy_s(dst, sizeof(dst), "A String Literal");

// if you are not using Microsoft Visual C++:

#ifndef _MSC_VER
int strcpy_s(char* dst, int dst_len, const char* src) {
  int i;
  if (dst == 0)
    return 0;

  if (dst_len == 0)
    return 0;

  if (src == 0) {
    dst[0] = 0;
    return 0;
  }

  for (i=0; i < src_len-1; i++) {
   if (src[i] == 0) {
      break;
   }
   src[i] = dst[i];
  }
  src[i] = 0;
  return i;
}
#endif
'; strncpy(array, tmp, 49); // copy a maximum of 49 characters

回答by E_net4 the Rustacean

Use strcpy, it is pretty much well documented and easy to find:

使用strcpy,它有很好的文档记录并且很容易找到:

##代码##

But of course, you must make sure that the length of the string that you are copying is smaller than the size of the array. An alternative then is to use strncpy, which gives you an upper limit of the bytes being copied. Here's another example:

但是当然,您必须确保要复制的字符串的长度小于数组的大小。另一种方法是使用strncpy,它为您提供了被复制字节的上限。这是另一个例子:

##代码##

If the string is greater than 49, the array will still have a well-formed string, because it is null-terminated. Of course, it will only have the first 49 characters of the array.

如果字符串大于 49,数组仍将具有格式良好的字符串,因为它以空值结尾。当然,它只会有数组的前 49 个字符。

回答by Bill

##代码##

回答by Bill

You could also do it this way:

你也可以这样做:

char dst[40];

字符 dst[40];

snprintf(dst, sizeof(dst), "%s", "string literal");

snprintf(dst, sizeof(dst), "%s", "字符串文字");