9

I am currently using a C++ IDE for something that will need to work on C, and wanted to make sure that I won't have problems with this later on. After making the struct below:

typedef struct test {
   int a;
   int b;
};

I then create an instance of it using test my_test; then stuff like my_test.a = 5, etc... and this works fine in my VStudio C++. Is this going to work on gcc later on?

I read the related questions that popped up (I see I am not the first person with this kind of question, either) but no one seemed to use the way I did.

In fact, what is the difference between typedef struct {//stuff} test; and my version?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
user472875
  • 3,115
  • 4
  • 37
  • 68
  • You can compile your program by opening command prompt and typing: `gcc FilePath/foo.c`. –  Nov 02 '10 at 23:29
  • 2
    Possible duplicate of [typedef struct vs struct definitions](http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions) I wrote an extensive answer on the differences and the concrete and actual meaning of each of those two constructs in C and C++ as an answer to the linked question. Read it for details not available in the accepted answer. – David Rodríguez - dribeas Nov 02 '10 at 23:57

3 Answers3

22
typedef struct THIS_IS_A_TAG
{
    int a;
    int b;
} THIS_IS_A_TYPEDEF;

THIS_IS_A_TYPEDEF object1;     // declare an object.       C:Ok,     C++:Ok
struct THIS_IS_A_TAG object2;  // declare another object.  C:Ok,     C++:Ok
THIS_IS_A_TAG object3;         // declare another object.  C:Not Ok, C++:Ok

The reason for the typedef is because C programmers would like to be able to do that third thing, but they can't.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
19

The difference between:

struct Name {};

And

typedef struct Name {} Name;

Is that, in C, you need to use:

struct Name instance_name;

With the former, whereas with the latter you may do:

Name instance_name;

In C++, it is not necessary to repeat the struct keyword in either case. Note that your example in which you create a typedef with no name (i.e. typedef struct Name{};) is non-standard AFAIK (if you use the keyword typedef, then you need to supply an alias to which to typedef that name).

As for the last variation:

typedef struct { /* ... */ } Name;

The code above creates an unnamed struct that is aliased to Name. You would use such a struct just the same way you would with typedef struct Name { /* ... */ } Name;, however compilers often emit the name of the struct (not the alias), and so you may get better error messages involving the struct if you give it a name and typedef that as opposed to typedef'ing an anonymous struct.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • I just noticed that "Warning 'typedef ' : ignored on left of 'test' when no variable is declared" is what I get when I do things the way I wrote in my original code snippet. Which begs the question what does the thing I wrote even do? – user472875 Nov 02 '10 at 23:35
  • @user472875: Nothing. It's like writing "typedef int;". Or like writing a definition in a dictionary without giving the word that means that thing. – Benjamin Lindley Nov 02 '10 at 23:55
8

In both C and C++, the example construct is modestly pointless:

typedef struct test {
   int a;
   int b;
};

In C, this says there is a type struct test with the two integers as content. If there was a name between the close brace '}' and the semi-colon ';', then you would get some benefit from the keyword typedef; as it stands, the keyword typedef is redundant, and (if set fussy enough), GCC will warn you about it.

In C++, this says there is a type struct test; further, in C++, it creates a type test too (which does not happen in C). The keyword typedef can still be left out and the same result will be achieved.

The syntax is legal; it is not useful, that's all. The keyword typedef can be omitted without changing the program's meaning in the slightest.

You can do:

typedef struct test {
   int a;
   int b;
} test;

Now, in both C and C++, you have a type struct test and an alias for it test.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278