0

Possible Duplicate:
Difference between 'struct' and 'typedef struct' in C++?

can someone explain what's the difference between the following definitions of Struct

typedef struct
{
}GOOD;

&

struct GOOD
{
};

&

typedef struct tagGOOD
{
}GOOD;

My teacher uses the last example, but i don't really know why he uses a "tag" on the struct name (if is it's name), can someone make it clear please?

Community
  • 1
  • 1
Yuri Freire
  • 35
  • 1
  • 1
  • 6

5 Answers5

2

In C and C++, any struct you declare with a name can be referred to as struct GOOD. You can also declare anonymous structs that do not have a name, as is done in the first example.

Any type can be typedef'd giving it an alternate name. In the first example, you're simply giving the name GOOD (without struct) to an anonymous struct. In the second example, your struct can only be accessed as struct GOOD. In the third, you can access it as either GOOD or struct GOOD.

All three options are legal, and which one to use is a matter of style.

epsalon
  • 2,294
  • 12
  • 19
2

Throughout all the code I've read over the years, I've seen all of these variations used. I just checked in VS2010 and they seemed to be equivalent. What I can tell you though is that the typedef keyword used to be required in C, but no longer in C++.

Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • Correct answer. Either the teacher is stuck in ~1992 when C and C++ diverged, or the teacher is teaching C and the question is mis-tagged. – MSalters Sep 13 '12 at 12:20
  • The typedef was **not** required in C. It was (and is) used as a convenience, so that you don't have to say `struct foo` all the time. – Pete Becker Sep 13 '12 at 12:21
1

Structures are supposed to have a name:

struct StructName {
   // stuff
};

Typedefs take a type and give it a name:

typedef SomeType NameYouWannaGiveThatType;

The third example combines these.

The example is an unnamed struct, which some compilers get upset about.

The second example is a named struct but you traditionally couldn't refer to it without including the word struct. eg

struct MyStruct {
    //...
};

MyStruct iffy;          // Not allowed in C, in my experience anyway
struct MyStruct good;   // No problem.

I haven't found this to be a problem in C++, but I'm sure there's people out there who know the C++ spec backwards and will give you a better answer. Anyway, you get around this with typedef.

typedef MyStruct { /* etc */ } MyStruct;

MyStruct s;

Personally, and this is probably just a matter of style, I prefer to not to use the same name:

typedef MyStruct { /* etc */ } SMyStruct;
paddy
  • 60,864
  • 6
  • 61
  • 103
1

If you are talking about C++, I believe typedef is not really needed. The story in fact comes from C.

in C, when I declare

struct GOOD_STRUCT {
   ...
}

When I want to declare a variable of this struct, I need to do

struct GOOD_STRUCT good;

In order to have a better coding style and readability, people used to typedef on this:

typedef GOOD_STRUCT GOOD;

so you can do

GOOD good;

To combine it both, people used to do

typedef struct GOOD_STRUCT {
   ...
} GOOD;

or

typedef struct {
   ...
} GOOD;

However, in C++, it is no longer needed. Once you have declare struct GOOD, you can already refer it by GOOD directly.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
0

Definition 1:

Defines an anonymous (empty) struct, and gives it a typedef of GOOD. This type can be referred to simply as GOOD in the future.

Definition 2:

Defines an (empty) struct named GOOD. In C and C++ this can be referred to as struct GOOD, and in C++ this can be referred to as GOOD.

Definition 3:

Defines an (empty) struct named tagGOOD and a typedef named GOOD. In C and C++ the struct can be referred to as both struct tagGOOD and GOOD, and in C++ this can be referred to as tagGOOD.

Definition 3 tends to be the most common in C code in my experience. People tend not to like anonymous structs very much, so they give the struct a name, but they also dislike using the struct keyword when referring to the type, so they give it a typedef as well. In C++ the typedef form is fairly pointless.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347