enum within structure??

S

sethukr

can we define enum within a structure??

for example,


typedef enum {red,blue}color;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??

-Sethu
 
I

Ian Collins

can we define enum within a structure??

for example,


typedef enum {red,blue}color;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??
Like this?

struct X {
enum Colour { red, blue } colour;
};

int main(void)
{
struct X x;

x.colour = blue;

return 0;
}
 
B

bluejack

can we define enum within a structure??

for example,

typedef enum {red,blue}color;

struct s1{
color c;

}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??

Sure. Nothing wrong with that. Are you having a problem? Maybe post
your code.
 
S

santosh

can we define enum within a structure??

for example,

typedef enum {red,blue}color;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible??

Yes.

An alternative is to do away with the typedef and give a name for the
enumeration type.
 
S

Servé Laurijssen

can we define enum within a structure??

for example,


typedef enum {red,blue}color;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??

It is possible because you can consider the type of an enum as int so it's
no more special than having an int member.

Beware though that if you do something like

struct X
{
enum Colour { red, green } col;
};

The enum Colour is actually *not* an embedded type as it would be in C++ so
the following should still compile

Colour c = green;

This would be an error in C++
 
K

Keith Thompson

Servé Laurijssen said:
Beware though that if you do something like

struct X
{
enum Colour { red, green } col;
};

The enum Colour is actually *not* an embedded type as it would be in C++ so
the following should still compile

Colour c = green;

This would be an error in C++

It would be an error in C as well, but this:

enum Colour c = green;

would be legal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top