enum within structure??

Discussion in 'C Programming' started by sethukr@gmail.com, Mar 9, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    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.
     
    Ian Collins, Mar 9, 2007
    #2
    1. Advertising

  3. bluejack Guest

    On Mar 8, 11:22 pm, "" <> 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.
     
    bluejack, Mar 9, 2007
    #3
  4. santosh Guest

    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.
     
    santosh, Mar 9, 2007
    #4
  5. Guest

    Thanks to all of u
    -Sethu
     
    , Mar 9, 2007
    #5
  6. <> wrote in message
    news:...
    > 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++
     
    Servé Laurijssen, Mar 9, 2007
    #6
  7. "Servé Laurijssen" <> 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) <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"
     
    Keith Thompson, Mar 9, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    591
  2. Jerminia
    Replies:
    3
    Views:
    659
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    507
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    458
    Dave Thompson
    Jun 10, 2004
  5. Replies:
    1
    Views:
    230
Loading...

Share This Page