473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Sep 4 '08 #1
4 8645
Samantha <sa***************@gmail.comwrites:
Hi - I am seeing enum members being used, with just the member's
name, and not the enum name.
That's how they work.
for example:
Expand|Select|Wrap|Line Numbers
  1.       typedef enum Fruit
  2.       {
  3.          Apple = 0,
  4.          Orange,
  5.          Banana = 0xFF
  6.        } FruitIndex;
  7.       int arrayInt[Orange]
  8.       {
Expand|Select|Wrap|Line Numbers
  1.  
  2. This is not valid syntax.
  3.  
  4.         
  5.                         more code here.....
  6.      }
  7.  
  8.  
>

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.
Question 2 - what do you think the 0xFF hex is fox??
Sorry, no idea. Looks like an arbitrary example value.
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.
Sep 5 '08 #2
On Sep 4, 4:58 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Samantha <samantha.domvi...@gmail.comwrites:
Hi - I am seeing enum members being used, with just the member's
name, and not the enum name.

That's how they work.
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.                        int arrayInt[Orange]
  3.        {
  •  
  • This is not valid syntax.
  •         
  •                          more code here.....
  •  
  •         
  •                       }
  •  
  •  
  • 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.
    Question 2 - what do you think the 0xFF hex is fox??

    Sorry, no idea. Looks like an arbitrary example value.
    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!
    Sep 5 '08 #3
    Samantha <sa***************@gmail.comwrites:
    On Sep 4, 4:58 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
    >Samantha <samantha.domvi...@gmail.comwrites:
    Hi - I am seeing enum members being used, with just the member's
    name, and not the enum name.

    That's how they work.
    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.                       int arrayInt[Orange]
    3.       {
  •  
  • This is not valid syntax.
  •         
  •                         more code here.....
  •  
  •         
  •                      }
  •  
  •  
  • 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.
    Question 2 - what do you think the 0xFF hex is fox??

    Sorry, no idea. Looks like an arbitrary example value.
    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>
    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.
    Sep 5 '08 #4
    Samantha <sa***************@gmail.comwrites:
    [...]
    >Samantha <samantha.domvi...@gmail.comwrites:
    [...]
    typedef enum Fruit
    {
    Apple = 0,
    Orange,
    Banana = 0xFF
    } FruitIndex;
    [...]
    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) ks***@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"
    Sep 5 '08 #5

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    12
    by: Steven T. Hatton | last post by:
    Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using...
    4
    by: Chris | last post by:
    I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
    18
    by: Nebula | last post by:
    Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
    21
    by: Bilgehan.Balban | last post by:
    Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in...
    4
    by: Nikhil Patel | last post by:
    Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
    13
    by: Don | last post by:
    How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
    2
    by: Jay | last post by:
    I have a few enums, a shortened example of one is: enum ParamTest {Err, Start, Block, Hold} Is there a programmatic way to find the number of "members" (probably the wrong C# term) of the Enum...
    12
    by: Robert Fuchs | last post by:
    Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
    5
    by: dissectcode | last post by:
    Hi - I am seeing enum members being used, with just the member's name, and not the enum name. for example: typedef enum Fruit { Apple = 0, Orange, Banana = 0xFF } FruitIndex;
    0
    by: ryjfgjl | last post by:
    In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    0
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    0
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    0
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
    0
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
    0
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.