Help | Site Map
Connecting Tech Pros Worldwide
  •         
  •  
  •                       }
  •  
  •         
  •     
  •     
  •  
     
    LinkBack Thread Tools
      #1  
    Old September 5th, 2008, 12:45 AM
    Samantha
    Guest
     
    Posts: n/a
    Default Accessing enum members...

    Hi - I am seeing enum members being used, with just the member's
    name, and not the enum name.

    for example:
    Expand|Select|Wrap|Line Numbers
    1. typedef enum Fruit
    2. {
    3. Apple = 0,
    4. Orange,
    5. Banana = 0xFF
    6. } FruitIndex;
    7.  
    8.  
    9. int arrayInt[Orange]
    10. {
    11. more code here.....
    12.  
    13. }
    14.  

    Question 1 - Is Orange's index = 1 ??
    Question 2 - what do you think the 0xFF hex is fox??
    QUESTION 3 - in the array - why don't I have to put
    [FruitIndex.Orange] to access the member??

    thanks!
    p.s. this is NOT a homework question - i am at work not understanding
    how this stuff works.
  •         
  •  
  •                         more code here.....
  • >
  •      }
  •  
  •         
  •     
  •     
  •   #2  
    Old September 5th, 2008, 01:05 AM
    Ben Bacarisse
    Guest
     
    Posts: n/a
    Default Re: Accessing enum members...

    Samantha <samantha.domville@gmail.comwrites:
    Quote:
    Hi - I am seeing enum members being used, with just the member's
    name, and not the enum name.
    That's how they work.
    Quote:
    for example:
    Expand|Select|Wrap|Line Numbers
    1.       typedef enum Fruit
    2.       {
    3.          Apple = 0,
    4.          Orange,
    5.          Banana = 0xFF
    6.        } FruitIndex;
    7. >
    8. >
    9.       int arrayInt[Orange]
    10.       {
    Expand|Select|Wrap|Line Numbers
    1.  
    2. This is not valid syntax.
    3.     Quote:
  •     
  •     
  • Quote:
    >
    >
    Question 1 - Is Orange's index = 1 ??
    Yes, though many people would say mixing explicit values and implicit
    ones is not good style. There are two common cases: either you want
    to choose the values because they matter to the program or you don't
    care so long as they are different. There are valid reasons to mix,
    but they are not common.
    Quote:
    Question 2 - what do you think the 0xFF hex is fox??
    Sorry, no idea. Looks like an arbitrary example value.
    Quote:
    QUESTION 3 - in the array - why don't I have to put
    [FruitIndex.Orange] to access the member??
    That's just how the language works. The semantics of C's enums are
    very weak.

    --
    Ben.
  •         
  •  
  •                          more code here.....
  •  
  •         
  •     
  •     
  •   #3  
    Old September 5th, 2008, 01:15 AM
    Samantha
    Guest
     
    Posts: n/a
    Default Re: Accessing enum members...

    On Sep 4, 4:58 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
    Quote:
  •         
  •  
  •                        int arrayInt[Orange]
  •        {
  •  
  •         
  •     
  •     
  • Samantha <samantha.domvi...@gmail.comwrites:
    Quote:
    Hi - I am seeing enum members being used, with just the member's
    name, and not the enum name.
    >
    That's how they work.
    >
    Quote:
    for example:
    Expand|Select|Wrap|Line Numbers
    1.        typedef enum Fruit
    2.        {
    3.           Apple = 0,
    4.           Orange,
    5.           Banana = 0xFF
    6.         } FruitIndex;
    Expand|Select|Wrap|Line Numbers
    1. >
    2.     Quote:
  •     
  •     
  • >
  • This is not valid syntax.
  • >
  •     Quote:
  •     
  •     
  • >
  •     Quote:
  •     
  •     
  • >
    Quote:
    Question 1 - Is Orange's index = 1 ??
    >
    Yes, though many people would say mixing explicit values and implicit
    ones is not good style. There are two common cases: either you want
    to choose the values because they matter to the program or you don't
    care so long as they are different. There are valid reasons to mix,
    but they are not common.
    >
    Quote:
    Question 2 - what do you think the 0xFF hex is fox??
    >
    Sorry, no idea. Looks like an arbitrary example value.
    >
    Quote:
    QUESTION 3 - in the array - why don't I have to put
    [FruitIndex.Orange] to access the member??
    >
    That's just how the language works. The semantics of C's enums are
    very weak.
    >
    --
    Ben.

    You are right - I meant to have arrayInt[] = { }

    So, what is the point of having created a enum name FruitIndex with a
    member Orange, if we don't use FruitIndex as an entity?

    thanks!
  •         
  •  
  •                      }
  •  
  •         
  •     
  •     
  •   #4  
    Old September 5th, 2008, 01:35 AM
    Ben Bacarisse
    Guest
     
    Posts: n/a
    Default Re: Accessing enum members...

    Samantha <samantha.domville@gmail.comwrites:
    Quote:
  •         
  •  
  •                         more code here.....
  •  
  •         
  •     
  •     
  • On Sep 4, 4:58 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
    Quote:
  •         
  •  
  •                       int arrayInt[Orange]
  •       {
  •  
  •         
  •     
  •     
  • >Samantha <samantha.domvi...@gmail.comwrites:
    Quote:
    Hi - I am seeing enum members being used, with just the member's
    name, and not the enum name.
    >>
    >That's how they work.
    >>
    Quote:
    for example:
    Expand|Select|Wrap|Line Numbers
    1.       typedef enum Fruit
    2.       {
    3.          Apple = 0,
    4.          Orange,
    5.          Banana = 0xFF
    6.        } FruitIndex;
    Expand|Select|Wrap|Line Numbers
    1. >>
    2.     Quote:
  •     
  •     
  • >>
  • >This is not valid syntax.
  • >>
  •     Quote:
  •     
  •     
  • >>
  •     Quote:
  •     
  •     
  • >>
    Quote:
    Question 1 - Is Orange's index = 1 ??
    >>
    >Yes, though many people would say mixing explicit values and implicit
    >ones is not good style. There are two common cases: either you want
    >to choose the values because they matter to the program or you don't
    >care so long as they are different. There are valid reasons to mix,
    >but they are not common.
    >>
    Quote:
    Question 2 - what do you think the 0xFF hex is fox??
    >>
    >Sorry, no idea. Looks like an arbitrary example value.
    >>
    Quote:
    QUESTION 3 - in the array - why don't I have to put
    [FruitIndex.Orange] to access the member??
    >>
    >That's just how the language works. The semantics of C's enums are
    >very weak.
    >>
    >--
    >Ben. Better to edit the message you are replying to -- in particular, snip
    the sig.

    <snip>
    Quote:
    So, what is the point of having created a enum name FruitIndex with a
    member Orange, if we don't use FruitIndex as an entity?
    Not much. In fact many people don't use C's enums at all. I think
    they help a little, but you may decide that they don't help enough.
    Of course, the alternative is even weaker:

    #define ORANGE 0
    #define APPLE 1
    ....

    and so on.

    --
    Ben.
      #5  
    Old September 5th, 2008, 02:05 AM
    Keith Thompson
    Guest
     
    Posts: n/a
    Default Re: Accessing enum members...

    Samantha <samantha.domville@gmail.comwrites:
    [...]
    Quote:
    Quote:
    >Samantha <samantha.domvi...@gmail.comwrites:
    [...]
    Quote:
    Quote:
    Quote:
    typedef enum Fruit
    {
    Apple = 0,
    Orange,
    Banana = 0xFF
    } FruitIndex;
    [...]
    Quote:
    So, what is the point of having created a enum name FruitIndex with a
    member Orange, if we don't use FruitIndex as an entity?
    for the same reason we have the name "int" even though we don't write
    "int.42".

    Either "enum Fruit" or "FruitIndex" is the name of the type you've
    created. You can, for example, declare objects of that type:

    FruitIndex obj;

    obj = Banana;

    (Due to C's strange rules, the constant Banana is actually of type
    int, but you can assign its value to an object of type FruitIndex.)

    --
    Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     

    Bookmarks

    « Previous Thread | Next Thread »
    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 Off
    [IMG] code is Off
    HTML code is Off
    Trackbacks are On
    Pingbacks are On
    Refbacks are On

    What is Bytes?

    We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
    Post your question now . . .
    It's fast and it's free

    Popular Articles