简体   繁体   English

在C中将数据从一个结构复制到另一个结构

[英]copy data from one structure to another in C

If I have a structure defined like:如果我的结构定义如下:

struct image{
unsigned int width, height;
unsigned char *data;
};

And 2 variables of this type:和这种类型的 2 个变量:

struct image image1;
struct image image2;

I want to transfer the data from image1 to the data of image2(presuming image1 has some data written, and image2 has data allocated with malloc or calloc).我想将数据从 image1 传输到 image2 的数据(假设 image1 写入了一些数据,而 image2 有分配了 malloc 或 calloc 的数据)。 How can it be done?怎么做到呢? Thanks a lot.非常感谢。

Assuming it is undesirable that two instances of struct image are pointing to the same data then memcpy() cannot be used to copy the structs .假设不希望struct image的两个实例指向相同的data ,那么memcpy()不能用于复制structs To copy:复印:

  • allocate memory for destination struct为目标结构分配内存
  • allocate memory for destination data buffer based on source data根据源data为目标data缓冲区分配内存
  • assign width members分配width成员
  • memcpy() data members. memcpy() data成员。

For example:例如:

struct image* deep_copy_image(const struct image* img)
{
    struct image* result = malloc(sizeof(*result));
    if (result)
    {
        /* Assuming 'width' means "number of elements" in 'data'. */
        result->width = img->width;
        result->data = malloc(img->width);
        if (result->data)
        {
            memcpy(result->data, img->data, result->width);
        }
        else
        {
            free(result);
            result = NULL;
        }
    }
    return result;
}

did you try memcpy(&image1,&image2,sizeof(image));你试过 memcpy(&image1,&image2,sizeof(image));

edit: Alocate data for image2.data after that you have to strcpy(image2.data,image1.data) if data is null terminated, but if its not, then use memcpy with the size of data.编辑:为 image2.data 分配数据,如果数据为空终止,则必须 strcpy(image2.data,image1.data) ,但如果不是,则使用 memcpy 和数据大小。

Regards, Luka问候,卢卡

struct image image1;
struct image image2;

...

image2.width = image1.width;
image2.height = image1.height;

/* assuming data size is width*height bytes, and image2.data has enough space allocated: */

memcpy(image2.data, image1.data, width*height);

It's simple in C. Simply do the following (assuming image2 is uninitialized):这在 C 中很简单。只需执行以下操作(假设 image2 未初始化):

image2 = image1;  //there's no deep copy, pointed to memory isn't copied

You can assign one structure variable to other given they are of the same type.您可以将一个结构变量分配给其他结构变量,因为它们属于同一类型。 No need to copy piece-meal.无需复制零碎。 This is a useful feature of C.这是 C 语言的一个有用特性。

This has been discussed before:这个之前已经讨论过:

Assign one struct to another in C 在 C 中将一个结构分配给另一个结构

If all you want is duplicating the struct (ie creating a "shallow" copy):如果您想要的只是复制结构(即创建“浅”副本):

image2 = image1;

if you also want to copy the data pointed to by image1.data ("deep copy"), then you need to do that manually:如果您还想复制image1.data指向的数据(“深拷贝”),那么您需要手动执行此操作:

memcpy(image2.data, image1.data, image1.width * image1.height);

(assuming there are image1.width * image1.height bytes in the data, and there's enough space malloc() ated in image2.data for storing that.) (假设数据中有image1.width * image1.height字节,并且image2.data中有足够的空间malloc()来存储它。)

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

相关问题 结构在内部如何在C中工作? 如何将数据从一个结构复制到另一个结构? - How does the structure work in C internally? How is a data copy made from one structure to another? 在C中将数据从一个矩阵复制到另一个 - Copy data from one matrix to another in C 如何从一个结构指针复制数据到另一个结构成员 - how to do a copy of data from one structure pointer to another structure member 在指向结构的指针中将一个数组的内容复制到另一个数组中 | C - Copy the contents of one array into another within a pointer to a structure | C 如何在C中将一个数组的数据复制到另一个数组? - How to copy data of one array to another in C? 如何在C中的另一个结构中复制一个结构的内容 - How to copy the content of a structure​ in another structure in C 用c从一个txt文件复制到另一个 - Copy from one txt file to another with c 在C / C ++中将数据从一个文件复制到另一个文件的最快方法? - Fastest way to copy data from one file to another in C/C++? C流:直接将数据从一个流复制到另一个流,而不使用缓冲区 - C streams: Copy data from one stream to another directly, without using a buffer C 语言:将数据从一个 typedef 结构复制到另一个并返回一个值 - C language: copy data from one typedef struct to another and return a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM