• Advertisement
Sign in to follow this  

Defining structs

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have seen two ways of defining structs. One way is:
struct Struct_Name
{
     int a, b, c ...;
};
And here's another method I've seen:
struct Some_weird_name_here
{
     int a, b, c ...;
} Struct_Name;
What is the point of the second method? I've seen it used in a lot of places, from online artices to winuser.h. But what exactly is the purpose of using that method?

Share this post


Link to post
Share on other sites
Advertisement
The latter is equivalent to

struct Some_weird_name_here
{

int a, b, c ...;
};

struct Some_weird_name_here Struct_Name;

That is, it defines the struct and creates an instance of it.

You may also have come across something like

typedef struct Some_weird_name_here
{

int a, b, c ...;
} Struct_Name;

This is because in C (not in C++, though!), Some_weird_name_here alone is not a fully qualified name for the struct (you need to prefix it with the keyword struct, as I did in the previous example); with the aid of a typedef, you can simply use the typedef-ed name, instead. This is thus equivalent to


struct Some_weird_name_here
{

int a, b, c ...;
};

typedef Some_weird_name_here Struct_Name;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement