enum -- The compiler doesn't know what I'm talking about.

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, obviously, I don't know what I'm talking about. This LOOKS like examples of enums I've found both here and googling, but GCC objects.

enum tPacketType 
{
	IntroPacket,
	HelloPacket
};

typedef struct 
{
	uint8_t	Recognition ;
	tPacketType PacketType ;  
	uint8_t	SourceAddress[5]; 
	uint8_t	PacketNumber ; 
	uint8_t	Payload[24] ;
}BFPacket;

So, obviously, I'm intending to do something like

#define IntroPacket 0
#defile HelloPacket 1

And so on, and hoping some day I don't have to cross from 256 numbers to 257 or more.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hehe Notice the Freudian typo

I didn't edit it 'cause it was just too rich.

Oh, and to top it off, the capta is "nHALP."

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The enum process assigns the values, automatically (and with any arbitrary values it chooses). Otherwise, forget enum and just #define, like you show.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need this:

typedef struct
{
   uint8_t   Recognition ;
   enum tPacketType PacketType ; 
   uint8_t   SourceAddress[5];
   uint8_t   PacketNumber ;
   uint8_t   Payload[24] ;
}BFPacket; 

Or you could make it a typedef:

typedef enum tPacketType
{
   IntroPacket,
   HelloPacket
} tPacketType; 

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh. It likes

typedef enum  
{
	IntroPacket = 0,
	HelloPacket
}tPacketType;

typedef struct 
{
	uint8_t		Recognition ; 
	tPacketType PacketType ; 
	uint8_t		SourceAddress[5]; 
	uint8_t		PacketNumber ; 
	uint8_t		Payload[24] ;  
}BFPacket;

void InitPacket( BFPacket* NewPacket, tPacketType PT );

It wasn't happy till I moved "tPacketType" to after the definition of the enum.

Right?

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But you also needed the "typedef". Your original code would have worked with C++, but not C.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We already had the exact same thing here:
https://www.avrfreaks.net/index.p...

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Amazing. I googled, freaks-searched and never found that I'd already asked the same thing.

Yep, it definitely doesn't like

typedef enum  tPacketType
{
	IntroPacket = 0,
	HelloPacket
};

typedef struct 
{
	uint8_t		Recognition
	tPacketType PacketType ; 
	uint8_t		SourceAddress[5]; 
	uint8_t		PacketNumber ;
	uint8_t		Payload[24] ;     
}BFPacket;

but is happy with

typedef enum  
{
	IntroPacket = 0,
	HelloPacket
} tPacketType;

typedef struct 
{
	uint8_t		Recognition
	tPacketType PacketType ; 
	uint8_t		SourceAddress[5]; 
	uint8_t		PacketNumber ;
	uint8_t		Payload[24] ;     
}BFPacket;

So, it seems, to do a typedef, you put

typedef thedefinitionofthetype nameofyourtype;

And let the gurus argu about whether enum, or even typedef should really be a part of C.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Yep, it definitely doesn't like

You need to learn how to typedef structs and enums. There are two possible parts/names to them:

typedef enum foo_tag {
  ITEM0,
  ITEM1,
  ITEM2
} foo_type_name;

typedef struct bar_tag {
  int n;
  foo_type_name foo_var;
  enum foo_tag foo_var2;
} bar_type_name;

struct bar_tag bar1;
bar_type_name bar2;

If you use the name before the {} (the "tag") then when you use it you have to say what it is "enum tag"/"struct tag".

If you use the name you give after {} then (in a typedef) then that is a full type name and the compiler knows it's an enum or a struct already.

For this reason I never see the point in tags. I suppose you might use it if a struct has a self-referential pointer field (for linked lists etc).