Accessing enum members... 
September 5th, 2008, 12:45 AM
| | |
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;
-
-
-
int arrayInt[Orange]
-
{
-
more code here.....
-
-
}
-
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. | 
September 5th, 2008, 01:05 AM
| | | | 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: -
typedef enum Fruit
-
{
-
Apple = 0,
-
Orange,
-
Banana = 0xFF
-
} FruitIndex;
-
>
-
>
-
int arrayInt[Orange]
-
{
| -
-
This is not valid syntax.
-
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. | 
September 5th, 2008, 01:15 AM
| | | | 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: -
typedef enum Fruit
-
{
-
Apple = 0,
-
Orange,
-
Banana = 0xFF
-
} FruitIndex;
| -
>>
-
This is not valid syntax.
-
>>
> 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! | 
September 5th, 2008, 01:35 AM
| | | | 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: -
typedef enum Fruit
-
{
-
Apple = 0,
-
Orange,
-
Banana = 0xFF
-
} FruitIndex;
| -
>>>>
-
>This is not valid syntax.
-
>>>>
>> 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. | 
September 5th, 2008, 02:05 AM
| | | | 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" |  | | | | /bytes/about
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 225,702 network members.
|