0

I was wondering what the best way was to copy a struct. Most of the info I was able to find online was about just copying the pointer to the struct. But what if I wanted to make a complete copy of a struct? One that I can modify without affecting the original?

This is sort of related to PSET4 resize. I wanted to copy the BITMAPFILEHEADER and BITMAPINFOHEADER so that I could pass that into the new file with changed values without affecting the original. What I ended up doing was just creating a new struct and copying the values one by one. Is there a function or something that allows me to quickly make a copy?

BTW questions similar to this one got responses saying you don't need to deal with structs for this PSET, just make an array, I get that, but I would like to know how to copy a struct regardless.

// update outfile BITMAPINFOHEADER
BITMAPINFOHEADER new_bi;


new_bi.biSize = bi.biSize;

new_bi.biWidth = n * bi.biWidth;
new_bi.biHeight = n * bi.biHeight;

new_bi.biPlanes = bi.biPlanes;
new_bi.biBitCount = bi.biBitCount;
new_bi.biCompression = bi.biCompression;

// determine padding for new scanlines
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

new_bi.biSizeImage = (((new_bi.biWidth * sizeof(RGBTRIPLE)) + new_padding) * -(new_bi.biHeight));

new_bi.biXPelsPerMeter = bi.biXPelsPerMeter; 
new_bi.biYPelsPerMeter = bi.biYPelsPerMeter; 
new_bi.biClrUsed = bi.biClrUsed; 
new_bi.biClrImportant = bi.biClrImportant; 

// update outfile BITMAPFILEHEADER    
BITMAPFILEHEADER new_bf;

new_bf.bfType = bf.bfType; 

new_bf.bfSize = (new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));

new_bf.bfReserved1 = bf.bfReserved1; 
new_bf.bfReserved2 = bf.bfReserved2; 
new_bf.bfOffBits = bf.bfOffBits; 
1

Oh, you're going to hate me after you read this, or you're going to slap yourself in the forehead and yell "DOH!"

BITMAPINFOHEADER new_bi = bi;

This copies the ENTIRE structure, provided that both vars are the same structure type.

The statements about not needing a structure require context. In modifying the headers, my opinion is that the safest way is to create a new structure that is a copy of the original and modify the new one, as you want to do. It may be slightly more efficient to modify the existing one, but the savings in resources is far outweighed by the safety of keeping the original intact. At least that's my opinion.

However, in actually manipulating the image data, a structure is probably not a good way to go. It can be done better in other ways, some with an array, others without. I prefer not to use an array or a structure, but to just process each line as needed, and as many times as needed.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance, and don't hit yourself in the head too hard. ;-)

| improve this answer | |
  • That's weird, I thought I tried that and it didn't work... That's why I wrote it all out. I then went online looking for a faster way and found all these posts about shallow copies, etc. My first thought was to just use the equal sign since I know that structs are like ints or chars. So I thought I did just that. I must have made a typo or maybe I forgot to add the variable type BITMAPINFOHEADER or something. Well all's well ends well! Thanks for the response! Actually what did you mean about "process each line as needed" How would you get at the variables without using the struct? – CS50_on_the_go Dec 22 '15 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .