tags:

views:

213

answers:

7

Hello,

I was using my structure like this. I don't like to typedef as I have told it can hide errors. However, I was looking at some sample code and I have seen structures declared like this. And this is the normal way I declare them.

struct person
{
     int age;
     char name[32];
};

using like this:

    struct person person_a;
    person_a.age = 20;
    etc.

However, I have seen structures declared like this:

struct 
{
     int age;
     char name[32];
}person;

and

struct _person
{
     int age;
     char name[32];
}person;

What is the difference between all these different techniques, and how would you decide when it is the best to use each particular one.

Many thanks for any suggestions,

+17  A: 

This:

struct 
{
    int age;
    char name[32];
}person;

declares a variable person that is a struct. The struct has no name, but you still use the person variable the same way.

struct _person
{
    int age;
    char name[32];
}person;

This is identical to:

struct _person
{
    int age;
    char name[32];
};
struct _person person;

It declares a struct named _person and then makes a variable called person of type struct _person.

Chris Lutz
+2  A: 

I think this discussion here will give you a fair idea.

Aamir
+1  A: 

There's no magic there really,

struct person
{
     int age;
     char name[32];
} var;

is the same thing as:

struct person
{
     int age;
     char name[32];
};
struct person var;
cloudhead
+1  A: 

The 1st code just creates the person struct.
The 2nd code creates an instance of the person struct called person_a.
The 3rd code creates an unnamed struct and an instance of it called person. The 4th code creates a struct named _person and an instance of it named person.

the_drow
+1  A: 

If the purpose of the structure definition is to declare a logical data structure that has meaning outside of the local module, then you would expect to see the first construct in a header file.

In real world C code, structures are often declared for purley pragmatic reasons that are local in scope. For example, formatting data that will be copied into a buffer. In these cases it's very handy and logical to merge the structure type and variable declaration. Also, using an anoymous declaration for the structure type avoids cluttering the namespace with a name that is not needed.

I do have one critisim of these examples: in C, names with a leading underscore are considered to be reserved for compiler vendors by convention. So I don't think the last example is consistent with best practice.

Paul Keister
+2  A: 

In both the last two structures, you've allocated storage for the structure called person.

What I mean is if you have something like this:

struct person
{
     int age;
     char name[32];
};

It's just a declaration; no variable allocation is there and hence cannot be used inside the code. You can start using this structure after you have declared as follows:

struct person p1;

Then, p1 can be utilized p1.age or p1.name, etc.

In terms of real-world code, instead of

struct _person
{
     int age;
     char name[32];
}person;

we usually see

typedef struct _person
{
     int age;
     char name[32];
} person_t;

In this case, we can save typing and more importantly the structure behaves like any other built-in type such as int, etc. For example,

person_t p1;

person_t *p1;

And, so on.

antreality
+2  A: 

For the first case (of second example), you will use this approach if you are sure that you do need another object for this structure. This looks clean and is fast to code :)

For the second case you have a ready object named 'person' and an option to create an object later on as

 struct _person object2

This is to explain why you will use either approach. The difference between them has been explained well above.

Naunidh