C语言 在 C 中将数据从一种结构复制到另一种结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16360781/
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 06:15:01  来源:igfitidea点击:

copy data from one structure to another in C

cstructurememcpy

提问by biggdman

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). How can it be done? Thanks a lot.

我想将数据从 image1 传输到 image2 的数据(假设 image1 有一些数据写入,而 image2 有分配给 malloc 或 calloc 的数据)。怎么做到呢?非常感谢。

回答by hmjd

Assuming it is undesirable that two instances of struct imageare pointing to the same datathen memcpy()cannot be used to copy the structs. To copy:

假设不希望有两个 的实例struct image指向同一个,datamemcpy()不能用于复制structs. 复印:

  • allocate memory for destination struct
  • allocate memory for destination databuffer based on source data
  • assign widthmembers
  • memcpy()datamembers.
  • 为目标结构分配内存
  • data根据源为目标缓冲区分配内存data
  • 指派width成员
  • 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;
}

回答by Luka Pivk

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

问候,卢卡

回答by Jomu

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

回答by Jomu

If all you want is duplicating the struct(i. e. creating a "shallow" copy):

如果您想要的只是复制结构(即创建“浅”副本):

image2 = image1;

if you also want to copy the datapointed to by image1.data("deep copy"), then you need to do that manually:

如果您还想复制("deep copy")指向的数据image1.data,则需要手动执行此操作:

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

(assuming there are image1.width * image1.heightbytes in the data, and there's enough space malloc()ated in image2.datafor storing that.)

(假设有image1.width * image1.height在数据字节,并且有足够的空间malloc()在atedimage2.data用于存储。)

回答by Rüppell's Vulture

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 中将一个结构分配给另一个