Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > enum within structure??

Reply
Thread Tools

enum within structure??

 
 
sethukr@gmail.com
Guest
Posts: n/a
 
      03-09-2007
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-09-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
bluejack
Guest
Posts: n/a
 
      03-09-2007
On Mar 8, 11:22 pm, "(E-Mail Removed)" <(E-Mail Removed)> 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??


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

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-09-2007
(E-Mail Removed) 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.

 
Reply With Quote
 
sethukr@gmail.com
Guest
Posts: n/a
 
      03-09-2007
Thanks to all of u
-Sethu

 
Reply With Quote
 
Servé Laurijssen
Guest
Posts: n/a
 
      03-09-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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++


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-09-2007
"Servé Laurijssen" <(E-Mail Removed)> writes:
[...]
> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments