Accessing enum members...

S

Samantha

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

     }


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.
 
B

Ben Bacarisse

Samantha said:
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]
{[/QUOTE]

This is not valid syntax.
 [QUOTE]
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.
 
S

Samantha

Samantha said:
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;[/QUOTE]
[QUOTE]
int arrayInt[Orange]
{[/QUOTE]

This is not valid syntax.
[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.
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.
[/QUOTE]



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!
 
B

Ben Bacarisse

Samantha said:
Samantha said:
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;[/QUOTE]
[QUOTE]
int arrayInt[Orange]
{[/QUOTE]

This is not valid syntax.
[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.
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.
[/QUOTE]

Better to edit the message you are replying to -- in particular, snip
the sig.

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.
 
K

Keith Thompson

Samantha said:
[...]

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top