Skip to main content
GameDev.net gamedev.net
🔒 Locked

about tagStruct {} Somestruct

Started by way_out_west Jul 22, 2003 at 5:34 AM 7 replies 1.3k views
Original Post
way_out_west
way_out_west
What''s the idea behind applying the ''tag'' prefix to the name of struct type when using typedef. For instance; in the code below, whats the point of using the typedef facility create a new name for an existing type if you are never going to use tagAccount anyway? I''m just curious, because I''ve noticed it everywhere in the win32 API and I just don''t see the reasoning for making a type that isn''t used and then create another name for that type and use that instead. typedef struct tagAccount { int Amount; } Account;
sbennett
sbennett
In C, structs have a seperate namespace to that of typedefs and intrinsic types. So, if the struct were simply declared as

struct Account
{
...
};

then, whenever you wanted to create an instance of it, you would have to type

struct Account myAccount;
struct Account *pAccount;

Hence, they use a typedef to get around this, allowing you to miss off the ''struct'' keyword when you declare an instance of it.

I''m not sure about this bit, but IIRC, C doesn''t allow nameless struct types, so it needs a ''dummy'' name that won''t conflict with the typedef in C++, where structs share the same namespace.
way_out_west
way_out_west
Makes perfect sense - 1 more question...

You mentioned that - ''In C, structs have a seperate namespace to that of typedefs and intrinsic types''.



Could ya explain that a little more? I know what namespace/lexical scope is but I don''t quite understand.

Thanks dude
sbennett
sbennett
In C, it would be quite legal to say:

struct Foo
{
int a, b, c;
};

typedef void(*Foo)();

Here, you have declared two types: a struct named Foo, and a typedef, also named Foo, which is a function pointer. If you then write

Foo Foo1;
struct Foo Foo2;

then Foo1 will be a function pointer, and Foo2 will be a struct containing three ints.

Confusingly enough, the following is also legal (I think...):

struct int
{
int a, b, c;
};


Not recommended though.
Way Walker
Way Walker
To be honest, I never understood the whole "tag" thing. For instance, this is perfectly legal C

typedef struct {
int amount; // I prefer variables to start with lowercase
} Account;
Account someAccount;

And will work as expected. The following is also legal C

typedef struct Account {
int amount;
} Account;
Account someAccount;
struct Account anotherAccount = someAccount;
struct { int amount; } oneLastAccount = anotherAccount;

This works because struct/union/enum tags are in a different namespace than typedef''ed names. The second-to-last line is legal because using the typedef''ed name and using the struct tag are the same thing. The last line is legal because "struct tag" is just a shorthand for writing out the whole struct.

On another note, I don''t believe that

struct int { int a, b, c; };

is legal because int is a reserved word, but it is true that int is not in the namespace of struct/union/enum tags.
cgoat
cgoat
The only problem I have with using nameless struct typedefs in C, like the above post, is that you can''t forward declare them to cut down on compilation time dependencies. Ok, well I haven''t found a way to do it. For example, look at SDL. They have:
typedef struct {
int x,y,w,h;
} SDL_Rect;
declared in SDL.h. Now if I want to have a class definition that accepts a reference to an SDL_Rect in one of the methods, I have to include SDL.h from that class'' header file. If it were defined as
typedef struct tagSDL_Rect {
int x,y,w,h;
} SDL_Rect;
then I could just forward declare it like so:
extern "C" {
struct tagSDL_Rect;
typedef struct tagSDL_Rect SDL_Rect;
};
which lets me move the actual include of SDL.h to the .cpp file instead, which prevents everyone that includes my class def from also needing to parse the SDL.h file and whatever else that includes.

It''s just a personal preference that I like to forward declare as much as possible, so this isn''t really a major problem. Just something I noticed.
Way Walker
Way Walker
quote:
Original post by cgoat
...why tags are good...



Interesting, maybe I''ll start using tags (when I use C). One question, though, why prefix it with "tag"? Why not just

typedef struct MyStruct {
// stuff...
} MyStruct;

?
way_out_west
way_out_west
In my Computer Science class, I heard that a ''tag'' is a label assigned to identify data in memory.

Btw thanks guys, I completely understand everything :D
sbennett
sbennett
OK, so the ''struct int'' thing isn''t legal after all. I didn''t have access to a compiler when I posted, so I couldn''t test it. Oh well...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.