There seems to be a confusion about the declaration.
When strategy
comes before {RANDOM, IMMEDIATE, SEARCH}
as in the following,
enum strategy {RANDOM, IMMEDIATE, SEARCH};
you are creating a new type named enum strategy
. However, when declaring the variable, you need to use enum strategy
itself. You cannot just use strategy
. So the following is invalid.
enum strategy {RANDOM, IMMEDIATE, SEARCH};
strategy a;
While, the following is valid
enum strategy {RANDOM, IMMEDIATE, SEARCH};
enum strategy queen = RANDOM;
enum strategy king = SEARCH;
enum strategy pawn[100];
When strategy
comes after {RANDOM, IMMEDIATE, SEARCH}
, you are creating an anonymous enum and then declaring strategy
to be a variable of that type.
So now, you can do something like
enum {RANDOM, IMMEDIATE, SEARCH} strategy;
strategy = RANDOM;
However, you cannot declare any other variable of type enum {RANDOM, IMMEDIATE, SEARCH}
because you have never named it. So the following is invalid
enum {RANDOM, IMMEDIATE, SEARCH} strategy;
enum strategy a = RANDOM;
You can combine both the definitions too
enum strategy {RANDOM, IMMEDIATE, SEARCH} a, b;
a = RANDOM;
b = SEARCH;
enum strategy c = IMMEDIATE;
Typedef
as noted before is used for creating a shorter variable declaration.
typedef enum {RANDOM, IMMEDIATE, SEARCH} strategy;
Now you have told compiler that enum {RANDOM, IMMEDIATE, SEARCH}
is synonomous to strategy
. So now you can freely use strategy
as variable type. You don't need to type enum strategy
anymore. The following is valid now
strategy x = RANDOM;
You can also combine Typedef along with enum name to get
typedef enum strategyName {RANDOM, IMMEDIATE, SEARCH} strategy;
There's not much advantage of using this method apart from the fact that you can now use strategy
and enum strategyName
interchangeably.
typedef enum strategyName {RANDOM, IMMEDIATE, SEARCH} strategy;
enum strategyName a = RANDOM;
strategy b = SEARCH;