• 10
• 9
• 12
• 14
• 14

# Defining structs

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

## 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 on other sites
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;