Lucky
Lucky

Reputation: 609

Can I use an enum as a struct name?

I think it may be called literal?

enum packet_structures{
   PacketOne,
   PacketTwo,
   PacketThree
};

struct PacketOne{
 unsigned int packet_id;
};
struct PacketTwo{
 unsigned int packet_id;
};

struct PacketThree{
 unsigned int packet_id;
};

And let's say I have a general packet.

struct PacketGeneral{
 packet_structures TypeOfPacket;
};

PacketGeneral newPacket;

newPacket.TypeOfPacket = PacketOne;

Can I literally use that enum's name to typecast a char* to a struct (i.e PacketOne)? Without having to typecast with (struct PacketOne), how can I just typecast that same struct but with just the enumeration newPacket.TypeOfPacket?

Upvotes: 2

Views: 172

Answers (2)

Tany
Tany

Reputation: 1322

No you cannot. Enums are used for storing literals and not identifiers.

Upvotes: 2

0xF1
0xF1

Reputation: 6116

No, at least not in C.

enum declares/defines constant identifiers.

So, you cannot use those same identifier again as structure tag name.

You can check that by compiling a program containing these declarations/definitions.

Upvotes: -1

Related Questions