Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 复制结构';他把他的内容传给另一个人_C_Struct_Copy_Ppm - Fatal编程技术网

C 复制结构';他把他的内容传给另一个人

C 复制结构';他把他的内容传给另一个人,c,struct,copy,ppm,C,Struct,Copy,Ppm,我正在尝试将一个结构的内容复制到同一类型的另一个结构中 我希望能够在不影响另一个结构的情况下更改一个结构的值 我正在处理读取和编辑PPM文件。我有一个结构: typedef struct { char format[4]; char comments[MAX_COMMENT_LENGTH]; int width, height, maxColourValue; PPMPixel **pixels; } PPMImage; 然后我有一个复制函数来复制这些值,但是在

我正在尝试将一个结构的内容复制到同一类型的另一个结构中

我希望能够在不影响另一个结构的情况下更改一个结构的值

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

typedef struct {
    char format[4];
    char comments[MAX_COMMENT_LENGTH];
    int width, height, maxColourValue;
    PPMPixel **pixels;
} PPMImage;
然后我有一个复制函数来复制这些值,但是在分配不同的字段时会出现错误

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

错误:

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;
复制功能:

//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;
}

我如何修正我的错误?
通过这种方式复制字段会达到我的目标吗?

您可以简单地执行a=b,其中和b是PPImage类型的变量。

您可以简单地执行a=b,其中和b是PPImage类型的变量。

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

void copyPPM(PPMImage *newPPM, PPMImage *messagePPM)  {
    *newPPM = *messagePPM;
}
这意味着您甚至不需要函数

然而,这些结构将共享
像素
数组。如果要复制该内容,则需要分配一个副本并复制内容

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

如果要制作结构的深度副本,需要通过以下方式为像素分配新阵列:

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]));
        }
    }
}
void copyPPM(PPMImage*newPPM,PPMImage*messagePPM){
*newPPM=*messagePPM;
如果(新PPM->像素){
newPPM->pixels=malloc(newPPM->height*sizeof(*newPPM->pixels));
对于(int i=0;iheight;i++){
newPPM->pixels[i]=malloc(newPPM->width*sizeof(*newPPM->pixels[i]);
memcpy(newPPM->pixels[i],messagePPM->pixels[i],
newPPM->width*sizeof(*newPPM->pixels[i]);
}
}
}

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

void copyPPM(PPMImage *newPPM, PPMImage *messagePPM)  {
    *newPPM = *messagePPM;
}
这意味着您甚至不需要函数

但是这些结构将共享
像素
数组。如果要复制该数组,则需要分配一个副本并复制内容

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

如果要制作结构的深度副本,需要通过以下方式为像素分配新阵列:

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]));
        }
    }
}
void copyPPM(PPMImage*newPPM,PPMImage*messagePPM){
*newPPM=*messagePPM;
如果(新PPM->像素){
newPPM->pixels=malloc(newPPM->height*sizeof(*newPPM->pixels));
对于(int i=0;iheight;i++){
newPPM->pixels[i]=malloc(newPPM->width*sizeof(*newPPM->pixels[i]);
memcpy(newPPM->pixels[i],messagePPM->pixels[i],
newPPM->width*sizeof(*newPPM->pixels[i]);
}
}
}


这只是复制地址,你的意思是*a=*b吗?那么在这种情况下,这是使a的字段指向b的字段所指向的还是它实际上复制了值?我对C是新手,仍然试图理解指针的全部内容。@CecilioPardo、
a
b
都是函数中的指针,所以你需要取消引用。@Porteous96:它是逐点复制结构,复制所有的值。@AlterMann:我知道,在我的回答中,我说它们是结构类型的变量。他不需要这样的函数。这只是复制地址,你的意思是*a=*b吗?那么在这种情况下,这是使a的字段指向b的字段,还是实际上复制了这些值?I“我是C新手,仍然在努力理解指针的全部内容。@CecilioPardo,
a
b
是函数中的指针,所以你需要取消引用。@Porteous96:复制结构,复制所有的值,这有点困难。@AlterMann:我知道,在我的回答中,我说它们是结构类型的变量。他不需要。”这样的函数。你应该使用memcpy来复制格式和注释字段的值。如果值以null结尾,你可以使用strcpy。这里已经给出了答案:-@bruceg谢谢你的提示,这解决了我的错误消息。你应该使用memcpy来复制格式和注释字段的值。如果值以null结尾,你可以使用strcpy空终止。这里已经回答了:-@bruceg谢谢你的提示,这解决了我的错误消息。我应该如何复制数组,以便在每个结构中都有它的版本?它是一个数组,每个单元格使用malloc,指向一个像素,因此它的工作原理类似于一个2d像素数组。我应该如何在每个像素上复制数组每个结构中都有它的一个版本吗?它是一个数组,每个单元格使用malloc,指向一个像素,所以它就像一个2d像素数组。