# That struct question I've always meant to ask

This topic is 4654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
ISTR it's for portability to C.

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 on other sites
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 on other sites
Thanks guys. That makes sense. [smile]