将结构的内容复制到另一个

时间:2021-09-14 19:57:33

I am trying to copy a struct's contents into another struct of the same type.

我试图将结构的内容复制到另一个相同类型的结构中。

I would like to be able to change the values of one struct without it affecting the other later though.

我希望能够更改一个结构的值,而不会影响另一个结构。

I am dealing with reading and editing PPM files. I have a struct:

我正在处理读取和编辑PPM文件。我有一个结构:

typedef struct {
    char format[4];
    char comments[MAX_COMMENT_LENGTH];
    int width, height, maxColourValue;
    PPMPixel **pixels;
} PPMImage;

And then I have a copy function to copy the values over but I get an error when assigning different fields.

然后我有一个复制功能来复制值,但在分配不同的字段时我收到错误。

I am trying to copy the fields of newPPM into messagePPM.

我正在尝试将newPPM的字段复制到messagePPM中。

Error:

错误:

incompatible types when assigning to type 'char[4]' from type 'char *'
    messagePPM->format = newPPM->format;
incompatible types when assigning to type 'char[100]' from type 'char *'
    messagePPM->comments = newPPM->comments;

Copy Function:

复制功能:

//A function to copy contents of one PPMImage to another
void copyPPM(PPMImage *newPPM, PPMImage *messagePPM) {

    messagePPM->format = newPPM->format;
    messagePPM->comments = newPPM->comments;
    messagePPM->width = newPPM->width;
    messagePPM->height = newPPM->height;
    messagePPM->maxColourValue = newPPM->maxColourValue;
    messagePPM->pixels = newPPM->pixels;

}

}

How do I fix my error? Will copying fields this way achieve what I am aiming for?

如何修复错误?以这种方式复制字段会实现我的目标吗?

2 个解决方案

#1


2  

You can copy the contents of one structure to the other with a simple assignment:

您可以使用简单的赋值将一个结构的内容复制到另一个结构:

void copyPPM(PPMImage *newPPM, PPMImage *messagePPM)  {
    *newPPM = *messagePPM;
}

This means you do not even need a function.

这意味着您甚至不需要功能。

Yet the structures will share the pixels array. If you want to duplicate that, you will need to allocate a copy and copy the contents.

然而,结构将共享像素阵列。如果要复制它,则需要分配副本并复制内容。

Copying one structure over another one may also cause the pixels array of the destination to be lost.

将一个结构复制到另一个结构上也可能导致目标的像素阵列丢失。

If you want to make a deep copy of the structure, you need to allocate new arrays for the pixels this way:

如果要对结构进行深层复制,则需要以这种方式为像素分配新数组:

void copyPPM(PPMImage *newPPM, PPMImage *messagePPM)  {
    *newPPM = *messagePPM;
    if (newPPM->pixels) {
        newPPM->pixels = malloc(newPPM->height * sizeof(*newPPM->pixels));
        for (int i = 0; i < newPPM->height; i++) {
            newPPM->pixels[i] = malloc(newPPM->width * sizeof(*newPPM->pixels[i]);
            memcpy(newPPM->pixels[i], messagePPM->pixels[i],
                   newPPM->width * sizeof(*newPPM->pixels[i]));
        }
    }
}

#2


0  

You can simply do a = b, where and b are variables of type PPImage.

你可以简单地做一个= b,其中和b是PPImage类型的变量。

#1


2  

You can copy the contents of one structure to the other with a simple assignment:

您可以使用简单的赋值将一个结构的内容复制到另一个结构:

void copyPPM(PPMImage *newPPM, PPMImage *messagePPM)  {
    *newPPM = *messagePPM;
}

This means you do not even need a function.

这意味着您甚至不需要功能。

Yet the structures will share the pixels array. If you want to duplicate that, you will need to allocate a copy and copy the contents.

然而,结构将共享像素阵列。如果要复制它,则需要分配副本并复制内容。

Copying one structure over another one may also cause the pixels array of the destination to be lost.

将一个结构复制到另一个结构上也可能导致目标的像素阵列丢失。

If you want to make a deep copy of the structure, you need to allocate new arrays for the pixels this way:

如果要对结构进行深层复制,则需要以这种方式为像素分配新数组:

void copyPPM(PPMImage *newPPM, PPMImage *messagePPM)  {
    *newPPM = *messagePPM;
    if (newPPM->pixels) {
        newPPM->pixels = malloc(newPPM->height * sizeof(*newPPM->pixels));
        for (int i = 0; i < newPPM->height; i++) {
            newPPM->pixels[i] = malloc(newPPM->width * sizeof(*newPPM->pixels[i]);
            memcpy(newPPM->pixels[i], messagePPM->pixels[i],
                   newPPM->width * sizeof(*newPPM->pixels[i]));
        }
    }
}

#2


0  

You can simply do a = b, where and b are variables of type PPImage.

你可以简单地做一个= b,其中和b是PPImage类型的变量。