Search code examples
cstructcopyppm

Copying a struct's contents to another


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:

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.

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?


Solution

  • 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]));
            }
        }
    }