enum within structure??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sethukr@gmail.com

    enum within structure??

    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

  • Ian Collins

    #2
    Re: enum within structure??

    sethukr@gmail.c om 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.

    Comment

    • bluejack

      #3
      Re: enum within structure??

      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.

      Comment

      • santosh

        #4
        Re: enum within structure??

        sethukr@gmail.c om 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.

        Comment

        • sethukr@gmail.com

          #5
          Re: enum within structure??

          Thanks to all of u
          -Sethu

          Comment

          • Servé Laurijssen

            #6
            Re: enum within structure??


            <sethukr@gmail. comwrote in message
            news:1173424936 .031550.265410@ c51g2000cwc.goo glegroups.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++


            Comment

            • Keith Thompson

              #7
              Re: enum within structure??

              "Servé Laurijssen" <ser@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_Keit h) kst-u@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"

              Comment

              Working...