Sign in to follow this  
CyberSlag5k

That struct question I've always meant to ask

Recommended Posts

CyberSlag5k    514
I'm about to start a new GBA project and have been reading through a few tutorials online to refresh my memory (it's been probably over a year since I've done any GBA dev) and came across this: typedef struct tagSprite { u16 attribute0; u16 attribute1; u16 attribute2; u16 attribute3; }OAM_Entry; Of course I've seen this style of struct declaration in the past, and have always wondered about it, but just never got around to asking. The only stupid question is the one not asked, right? So now I'm asking. If you were to declare an instance of that struct, you'd do something like this: OAM_Entry myOAM; But what's the purpose of the tagSprite identifier? Could you also do: tagSprite myOAM; To yield the same results? And regardless, what's the purpose of having two names for the struct? I've always declared my structs as such: struct OAM_Entry { u16 attribute0; u16 attribute1; u16 attribute2; u16 attribute3; }; Perhaps mine is the C++ method, whereas the previous is strictly C? Something tells me that's not the case, but regardless that other form doesn't seem to make as much sense. In fact, it looks as though you're defining the type tagSprite and doing a typedef on it to yield OAM_Entry, but to what end I'm not entirely sure. If that's the case, OAM_Entry and tagSprite should indeed be the same data type, and the typedef serves little purpose.

Share this post


Link to post
Share on other sites
Sandman    2210
ISTR it's for portability to C.

If your structs are defined

struct OAM_Entry
{
int bar;
};


and you then instantiate them with:


OAM_Entry foo;


Then trying to compile that code in a C compiler will cause it to fall over, because it would expect:


struct OAM_Entry foo;


instead. However, with the dummy name, you can use instantiate it the same way in both languages. This is particularly important in headers which are likely to be used in either language.

Share this post


Link to post
Share on other sites
Zahlman    1682
Yep. The "typedef struct idiom" is a C idiom, which is possible in C++ but entirely unnecessary except perhaps for interoperability. In the C code, you could write either "OAM_Entry foo" or "struct tagSprite foo". The C++ compiler is however smart enough to know that "tagSprite" is a struct name, and thus the keyword is not needed, and thus neither is the typedef.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this