enum in a struct

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

Why can't I put my enum in a struct 'lest it destruct? Clearly, I minunderstand something here:

enum IOType{Output, Input, InputPullup, Servo};
enum LabelT{lPin, lSwitch, lEvent};
typedef struct{
	LabelT LabelType;
	char * Name ;
	int Number ;
	struct Label * Next ;
} Label ;

I want to build a linked list at run time with each element indicating is it a pin, a switch or an event, a number and a pointer to the name somewhere's else in memory. But the compiler points to the LabelT LabelType in the struct and says it doesn't know what a LabelT is.

Or am I completely bonkers?

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

Either

typedef struct {
   enum LabelT LabelType;
   ...

or

typedef enum {lPin, lSwitch, lEvent} LabelT; 

Stefan Ernst

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

http://www.daniweb.com/software-...

Quote:
Hi everyone,
I am having trouble using enums in structs. I am developing for the linux platform, with the gcc compiler. Below is an example of the code that produces the error.

enum Day {Monday=0x01, Tuesday=0x02, Wednesday=0x03};
typedef struct {
int numberofDays;
Day weekday;
} Week;

When I compile, I get the error
line 4 error: expected specifier-qualifier-list before 'Day’

Does anyone have an idea what might be causing this? I expect that it should be possible to declare an enum outside the struct and use it within. I have tried changing the hexes to ints in the enum, but that seems not to be the problem.


Quote:

enum Day {Monday=0x01, Tuesday=0x02, Wednesday=0x03};
typedef struct {
int numberofDays;
enum Day weekday;
} Week;


http://www.lmgtfy.com/?q=enum+in...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Oh. I see. I hadn't made my enums typedefs so I could make variables of the types. In fact, I don't know what I made my enums.

typedef enum {Output, Input, InputPullup, Servo}IOType;
typedef enum {lPin, lSwitch, lEvent}LabelT;
typedef struct{
	LabelT LabelType;
	char * Name ;
	char * Number ;
	struct Label * Next ;
} Label ;

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:

I hadn't made my enums typedefs so I could make variables of the types.

You don't necessarily need to make typedefs to be able to create fields in a struct (although the practice is wise).

See sternsts two alternatives above.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

enums are standard part of C, they should (and most of the time will) "compress" themselves to the minimal memory space it can (rounded to 1 byte)

That means that any enum which has less than 256 entities, or which has no entities defines as a number greater that 255, will occupy one byte in memory and can be treated as uint8_t or even int8_t if you know what you are doing.

If you don't typedef the enums, just add the keyword "enum" in front of the actual enum name in all places where you use them:

void my_stupid_function(enum some_stupid_automatic_constants c);