Connecting Tech Pros Worldwide Forums | Help | Site Map

Accessing enum members...

Samantha
Guest
 
Posts: n/a
#1: Sep 5 '08
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.

Ben Bacarisse
Guest
 
Posts: n/a
#2: Sep 5 '08

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:
  •  
  •                         more code here.....
  • >
  •      }
  •  
  • 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.
    Samantha
    Guest
     
    Posts: n/a
    #3: Sep 5 '08

    re: Accessing enum members...


    On Sep 4, 4:58 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
    Quote:
    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:
  •  
  •                        int arrayInt[Orange]
  •        {
  •  
  • >
  • This is not valid syntax.
  • >
  •     Quote:
  •  
  •                          more code here.....
  •  
  • >
  •     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!
    Ben Bacarisse
    Guest
     
    Posts: n/a
    #4: Sep 5 '08

    re: Accessing enum members...


    Samantha <samantha.domville@gmail.comwrites:
    Quote:
    On Sep 4, 4:58 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
    Quote:
    >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:
  •  
  •                       int arrayInt[Orange]
  •       {
  •  
  • >>
  • >This is not valid syntax.
  • >>
  •     Quote:
  •  
  •                         more code here.....
  •  
  • >>
  •     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.
    Keith Thompson
    Guest
     
    Posts: n/a
    #5: Sep 5 '08

    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"
    Closed Thread