0

Trying to understand copy.c has been a pain for me so far. I feel complete lost at how the structures from the helper file work in copy.c An example:

Helper file: typedef struct { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } __attribute__((__packed__)) BITMAPFILEHEADER;

and...

Implementation: BITMAPFILEHEADER bf; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

If in the helper file, it is just a structure full of variables, how are you able to "point" to where it is in the file called by the pointer inptr? Does the program look for where this specific order of variables defined by the struct is located? Thanks.

1 Answer 1

2

Yes, you have it right. When a struct is declared, the type, size and location of each var in the struct is remembered. The order of those vars is also important. Physically, each var in the struct is located at a specific offset from the starting address of the struct. Also, each var is located immediately after the previous one within the struct.

It's also important to understand that the field names within the struct are not actually variable names. When a struct TYPE is created, no variables have been created yet. When a struct var is created, it contains fields with all those field names. These are not pointers to unique vars that exist somewhere else (unless, of course, the field really is defined as a pointer that points to something else.) The field names within a struct type don't identify a unique var created by a program, they only identify a field within the struct type. The names are reused for the fields in every var of that struct type, but the combination of the struct var name and the field name within that struct are what identify a unique value.

A struct is identified in a program by it's name, like bf is in the code you posted. If you wanted to access a specific field or var within that struct, you would use the . operator. Say that you wanted to access bfSize in the bf struct. You would use bf.bfSize. bf identifies the struct and bfSize identifies the field within the struct. If you also had bf2 and wanted to access it's bfSize field, use the same logic, bf2.bfSize. It's really that simple. Hopefully, this helps.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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