By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

enum within structure??

P: n/a
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

Mar 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
se*****@gmail.com wrote:
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;
}

--
Ian Collins.
Mar 9 '07 #2

P: n/a
On Mar 8, 11:22 pm, "seth...@gmail.com" <seth...@gmail.comwrote:
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.

Mar 9 '07 #3

P: n/a
se*****@gmail.com wrote:
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.

Mar 9 '07 #4

P: n/a
Thanks to all of u
-Sethu

Mar 9 '07 #5

P: n/a

<se*****@gmail.comwrote in message
news:11**********************@c51g2000cwc.googlegr oups.com...
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++
Mar 9 '07 #6

P: n/a
"Servé Laurijssen" <se*@n.tkwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.