Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accessing enum members...

0 views
Skip to first unread message

Samantha

unread,
Sep 4, 2008, 7:39:01 PM9/4/08
to
Hi - I am seeing enum members being used, with just the member's
name, and not the enum name.

for example:
[code]
typedef enum Fruit
{
Apple = 0,
Orange,
Banana = 0xFF
} FruitIndex;


int arrayInt[Orange]
{
more code here.....

}
[/code]


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

unread,
Sep 4, 2008, 7:58:05 PM9/4/08
to
Samantha <samantha...@gmail.com> writes:

> 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:
> [code]
> typedef enum Fruit
> {
> Apple = 0,
> Orange,
> Banana = 0xFF
> } FruitIndex;
>
>
> int arrayInt[Orange]
> {

This is not valid syntax.



> more code here.....
>
> }
> [/code]
>
>
> 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.

Samantha

unread,
Sep 4, 2008, 8:13:04 PM9/4/08
to
On Sep 4, 4:58 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:

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

unread,
Sep 4, 2008, 8:34:25 PM9/4/08
to
Samantha <samantha...@gmail.com> writes:

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.

Keith Thompson

unread,
Sep 4, 2008, 9:04:21 PM9/4/08
to
Samantha <samantha...@gmail.com> writes:
[...]
>> Samantha <samantha.domvi...@gmail.com> writes:
[...]

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

0 new messages