0

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?

Porteous96
  • 97
  • 2
  • 11
  • you should use memcpy to copy the values of the format and comments fields. You could use strcpy if the values are null terminated. – bruceg Feb 10 '16 at 23:14
  • This has been answered here:- http://stackoverflow.com/questions/4931123/copying-one-structure-to-another – BryanT Feb 10 '16 at 23:16
  • 1
    @bruceg Thanks for the tip, this solved my error message. – Porteous96 Feb 11 '16 at 01:40

2 Answers2

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]));
        }
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • How would I go about copying the array over so that I have a version of it in each struct? It's an array using malloc for each cell and that points to a pixel, so it works like a 2d array of pixels. – Porteous96 Feb 11 '16 at 02:02
0

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

Cecilio Pardo
  • 1,717
  • 10
  • 13