Structure Question

Started by
4 comments, last by TMP72 20 years, 5 months ago
I''m trying to understand this structure. I was hoping someone can explain the BITMAP_FILE, *BITMAP_FILE_PTR; part of the structure. This is from Tricks of the Windows Game Programming Gurus: typedef struct BITMAP_FILE_TAG { BITMAPFILEHEADER bitmapfileheader; BITMAPINFOHEADER bitmapinfoheader; PALETTEENTRY palette[256]; UCHAR *buffer; } BITMAP_FILE, *BITMAP_FILE_PTR; I was assuming that a variable of type BITMAP_FILE would be used, however in the book a function uses the data type BITMAP_FILE_PTR int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename); Then when calling the function the following is used: BITMAP_FILE bitmap; Load_Bitmap_File(&bitmap,"alley8.bmp") I guess I''m not following this C-style structure call.
Advertisement
You are allowed to declare variables of a struct type during the declaration of the structure. Basically all that's happening there is stating that, while you're making the structure, you're also declaring 2 variables, one an instantiation called BITMAP_FILE, the other a pointer to a BITMAP_FILE_TAG structure called BITMAP_FILE_PTR as indicated by the preceding *. You could also put a BITMAP_FILE_ARRAY[50] in there too if you wanted to create a 50 element array of BITMAP_FILE_TAG structures.

It's nothing special, it's just that usually people prefer to keep the declaration of variables of that structure type separate from the structure definition, like so:

BITMAP_FILE_TAG BITMAP FILE;
BITMAP_FILE_TAG *BITMAP_FILE_PTR;
BITMAP_FILE_TAGE BITMAP_FILE_ARRAY[50];

-fel

[edited by - felisandria on November 10, 2003 11:34:19 AM]
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
quote:Original post by felisandria
You are allowed to declare variables of a struct type during the declaration of the structure. Basically all that''s happening there is stating that, while you''re making the structure, you''re also declaring 2 variables, one an instantiation called BITMAP_FILE, the other a pointer to a BITMAP_FILE_TAG structure called BITMAP_FILE_PTR as indicated by the preceding *. You could also put a BITMAP_FILE_ARRAY[50] in there too if you wanted to create a 50 element array of BITMAP_FILE_TAG structures.

It''s nothing special, it''s just that usually people prefer to keep the declaration of variables of that structure type separate from the structure definition, like so:

BITMAP_FILE_TAG BITMAP FILE;
BITMAP_FILE_TAG *BITMAP_FILE_PTR;
BITMAP_FILE_TAGE BITMAP_FILE_ARRAY[50];

-fel

[edited by - felisandria on November 10, 2003 11:34:19 AM]


I think you missed the typedef from the example felisandria

Hes actually creating alternative names for the BITMAP_FILE_TAG structure and BITMAP_FILE_TAG pointers. Hence why he declares a variable as BITMAP_FILE bitmap;

Its just another example of how poorly Lamothe codes, never obvious what hes doing.
quote:Original post by felisandria
You are allowed to declare variables of a struct type during the declaration of the structure.

You can, but he isn''t; this is a typedef.

For the OP: A typedef creates an alias for a type, e.g. if I wanted to save on typing I could typedef unsigned long int ulong;
and henceforth use the type ulong as being perfectly equivalent to unsigned long int. In the example in your original post, BITMAP_FILE is typedef''ed to be equivalent to stuct BITMAP_FILE_TAG; BITMAP_FILE_PTR is defined as a pointer to a BITMAP_FILE_TAG. (Therefore, clearly, BITMAP_FILE* is equivalent to BITMAP_FILE_PTR, and the address of a variable of the former type is a valid value for a variable of the latter type.) This practice is rather common in C, but less so in C++, due to the fact that in C, the name of a struct alone is not a fully qualified name (it also needs the keyword struct if, for instance, you wish to declare a variable), which is not the case in C++.
Yep, I did miss the typedef. That''s what I get for doing 4 things at once hehe.

Both the end-of-function declarations and the end-of-function typedefing aren''t used very often, though. At least not in my experience.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Thanks Miserable,

Your explaination really cleared it up for me! Sometimes these things get tricky. I rather use a C++ style over this.

Hey Jingo,

Your right about Lamothe code being very poor. I''m just hoping to get the basics from his book and not fall into the trap of poor coding.

If anyone knows a better book to start with let me know.

Thanks Again for all the Help

Tom

This topic is closed to new replies.

Advertisement