If I have a structure defined like:
struct image{
unsigned int width, height;
unsigned char *data;
};
And 2 variables of this type:
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.
If all you want is duplicating the struct (i. e. creating a "shallow" copy):
image2 = image1;
if you also want to copy the data pointed to by image1.data
("deep copy"), then you need to do that manually:
memcpy(image2.data, image1.data, image1.width * image1.height);
(assuming there are image1.width * image1.height
bytes in the data, and there's enough space malloc()
ated in image2.data
for storing that.)