I"m a starting person in C programming, yet I to be wondering what"s the difference in between using typedef when specifying a structure versus not making use of typedef. It seems to me like there"s yes, really no difference, they attain the exact same goal.

You are watching: Which of the following struct definitions is correct in c++?

struct myStruct int one; int two;;vs.

typedef struct int one; int two;myStruct;

*

*

The common idiom is utilizing both:

typedef struct S int x; S;They are different definitions. To make the conversation clearer ns will break-up the sentence:

struct S int x; ;typedef struct S S;In the very first line you are defining the id S in ~ the struct name an are (not in the C++ sense). You have the right to use it and define variables or duty arguments that the recently defined type by specifying the kind of the debate as struct S:

void f( struct S discussion ); // struct is compelled hereThe 2nd line adds a kind alias S in the global name room and thus allows you to just write:

void f( S discussion ); // struct keyword no much longer neededNote that due to the fact that both identifier name spaces space different, specifying S both in the structs and global spaces is no an error, together it is no redefining the very same identifier, however rather creating a different identifier in a various place.

To make the distinction clearer:

typedef struct S int x; T;void S() // correct//void T() // error: symbol T already defined together an alias to "struct S"You can define a duty with the exact same name that the struct together the identifiers are kept in various spaces, yet you cannot specify a function with the exact same name together a typedef together those identifiers collide.

See more: Solved Determine Whether Each Change Represents Oxidation Or Reduction.

In C++, the is slightly different as the rules to find a symbol have readjusted subtly. C++ still keeps the two different identifier spaces, yet unlike in C, when you only specify the symbol in ~ the course identifier space, you space not required to administer the struct/class keyword:

// C++struct S int x; ; // S identified as a classvoid f( S a ); // correct: struct is optionalWhat alters are the search rules, not where the identifiers space defined. The compiler will certainly search the worldwide identifier table and also after S has not been uncovered it will search because that S in ~ the class identifiers.

The code presented prior to behaves in the same way:

typedef struct S int x; T;void S() // correct <*>//void T() // error: price T already defined as an alias come "struct S"After the an interpretation of the S role in the second line, the struct S cannot be resolved immediately by the compiler, and also to create things or specify an dispute of that type you have to fall earlier to consisting of the struct keyword: