简体   繁体   English

将分配的char数组复制到“普通” char数组

[英]copy allocated char array to “normal” char array

Please, tell me, what is the correct way of copying allocated char array to "normal" char array? 请告诉我,将分配的char数组复制到“普通” char数组的正确方法是什么? I have attempted to do the following, but it fails : 我尝试执行以下操作,但失败:

char * buf = (char *) malloc (BUFSIZ * sizeof(char));
// filling up the allocated array with stuff...
char temp[BUFSIZ];
strcpy(temp, buf); // strcpy doesn't work

What you probably want is strncpy() , which copies up to a certain number of bytes from a string. 您可能想要的是strncpy() ,它可以从字符串中复制最多一定数量的字节。

strncpy(temp, buf, BUFSIZ - 1);
temp[BUFSIZ - 1] = '\0'; // null terminate the string

If that too fails, possibly just use memcpy() 如果那也失败了,可能只是使用memcpy()

memcpy(tmp, buf, BUFSIZ - 1);
temp[BUFSIZ - 1] = '\0'; // null terminate the string

strcpy works with zero-terminated strings, not arbitrary lumps of memory. strcpy适用于以零结尾的字符串,而不是任意的内存块。

If you have filled it with a terminated string, then strcpy should work; 如果您用一个终止的字符串填充它,则strcpy应该可以工作; if it doesn't, please give more information about how it "doesn't work". 如果不行,请提供更多有关“行不通”的信息。

If you don't want a terminated string, then use memcpy : 如果您不希望使用终止的字符串,请使用memcpy

memcpy(temp, buf, BUFSIZ);

Note that there's no need to multiply by sizeof(char) , since that is 1 by definition. 注意,不需要乘以sizeof(char) ,因为根据定义,该乘数为1。

If you're actually writing C++, then you probably want to use std::vector or std::string rather than messing around with raw memory. 如果您实际上是在编写C ++,那么您可能想使用std::vectorstd::string而不是搞乱原始内存。

First things first, you should not cast the return value of malloc (in C anyway) since it can hide errors. 首先,您不应该malloc的返回值(无论如何,在C中),因为它可以隐藏错误。

Secondly, you never need to multiply by sizeof(char) since it's always guaranteed to be one - doing so clogs up your code. 其次,您永远不需要乘以sizeof(char)因为它始终保证为1-这样做会阻塞您的代码。

And, as to the actual question, you can use: 并且,对于实际问题,您可以使用:

memcpy (temp, buff, BUFFSZ);

to copy the entire character array. 复制整个字符数组。

I'm assuming that's what you want because you make no mention of handling C "strings", only a character array. 我假设这就是您想要的,因为您没有提到处理C“字符串”,而只是处理字符数组。

If indeed you are handling C strings, strcpy will work fine in this case, provided: 如果你确实正在处理C字符串, strcpy会在这种情况下正常工作,提供:

  • you have room at the end of the buffer for the terminating zero-byte; 您在缓冲区的末尾有空间可容纳终止的零字节; and
  • you've actually put the zero-byte in there. 您实际上已经零字节放入其中。

For example, this little snippet works fine: 例如,这个小片段可以正常工作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) {
    // Two buffers.

    char buff1[4];
    char *buff2 = malloc (4);
    if (buff2 == NULL) {
        puts ("No memory!");
        return 1;
    }

    // Populate first buffer.

    buff2[0] = 'p';
    buff2[1] = 'a';
    buff2[2] = 'x';
    buff2[3] = '\0';

    // Transfer and print.

    strcpy (buff1, buff2);
    puts (buff1);

    // Free and exit.

    free (buff2);
    return 0;
}

strcpy is a function that copy a string. strcpy是复制字符串的函数。

A string is a sequence of character terminated by and including the null character. 字符串是由空字符终止并包括空字符的字符序列。

 char *buf = malloc(BUFSIZ);

This malloc call allocates an array BUFSIZ of char but this is not a string. malloc调用分配char的数组BUFSIZ,但这不是字符串。 To copy an array use the memcpy function: 要复制数组,请使用memcpy函数:

memcpy(temp, buf, BUFSIZ);

If your array holds a string (a sequence of characters terminated by a null character), you can then use strcpy to copy it. 如果您的数组包含一个字符串(一个以空字符结尾的字符序列),则可以使用strcpy复制它。

You don't have a terminating NUL character in your buf . 您的buf没有终止NUL字符。 You should make sure to do buf[last_actually_used_char+1] = '\\0'; 您应该确保执行buf[last_actually_used_char+1] = '\\0'; . This means buf will have to be one character larger than the data you wish to store. 这意味着buf必须比要存储的数据大一个字符。

This is necessary because strcpy finds the length of the information to be copied by searching for a terminating NUL . 这是必需的,因为strcpy通过搜索终止NUL来查找要复制的信息的长度。

I strongly encourage the use of the safer strncpy , or if you just want to copy data (no '\\0' necessary) the faster and safer memcpy . 我强烈建议您使用更安全的strncpy ,或者如果您只是想复制数据 (不需要'\\0' ),则可以使用更快更安全的memcpy

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

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