1
typedef struct test { 
    int a;
}; 

int main(void) { 
test t; 
t.a = 3; 
} 

The above does not compile. However when I change the struct to:

typedef struct {
    int a; 
}test; 

Everything works fine. Why is this? I have seen plenty of code examples where the struct is on the same line as the typedef but it isn't compiling for me.

user2202911
  • 2,898
  • 5
  • 20
  • 28
  • 1
    http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions – darmat Sep 12 '13 at 17:23
  • See POWs answer below, you're missing the the alias-name. – Mike Makuch Sep 12 '13 at 17:25
  • @EricPostpischil Based on the answers I doubt it's legal C. Im getting errors on gcc – user2202911 Sep 12 '13 at 17:35
  • @user2202911: Sorry, I was addressing only the initial declaration. The initial declaration is legal C, but it does not declare a type name because there is no name in the position where a name should be. When you ask about something that “does not compile”, you should include the actual compiler error message. – Eric Postpischil Sep 12 '13 at 17:37
  • I understand now. It is a difference in C/C++ that i was unaware of. – user2202911 Sep 12 '13 at 17:45

3 Answers3

5

The general syntax for typedef is

 typedef type-declaration      alias-name;
          |                     |
          |                     |
          |                     |
 typedef struct {int a; }      test; //2nd one is correct
          |                     |              
          |                     |
 typedef struct test { int a;}    ;  //You missed the synonym/alias name here

Edit

See Eric Postpischil comments below

You'll just get a warning: useless storage class specifier in empty declaration

Ref: -this

Community
  • 1
  • 1
P0W
  • 46,614
  • 9
  • 72
  • 119
  • 2
    A Microsoft document is **not** authoritative about C. The C standard does not actually contain a separate syntax for `typedef`. `typedef` shares the same syntax as other declarations, and that syntax does not actually require a declarator. The requirement for a declarator is actually a constraint, not part of the formal grammar, per C 2011 (N1570) 6.7 2: “A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.” – Eric Postpischil Sep 12 '13 at 17:28
2

When you use typedef with structures, the typedef name is put after the structure. That's how C is specified to work.

If you use the first (without the typedef obviously), then you have to use the struct keyword, with the second you only have to use the name.

You can also use the same name for both the structure and the typedef like

typedef struct test { 
    int a;
} test;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

I conceptualize typedef as effectively working with two parameters:

typedef "original type"  "new type";

(I know this is not 100% accurate, but it is a useful way to view it for simplicity)

As in:

typedef unsigned int HRESULT;
// HRESULT is a new type that is the same as an unsigned int.

In your example:

typedef struct test { int a; }  (Missing Second Parameter!) ;

You've passed the first "param", a struct named test, but you never gave this type a new name with a second parameter.

What I think you want is:

typedef struct { int a; }  test;

Now you've taken a struct with a single field (a), and given it the name test.

abelenky
  • 63,815
  • 23
  • 109
  • 159