tags:

views:

594

answers:

6

I think I've read somewhere that it is illegal to take the address of an enum value in C (enum values not being lvalues; however, I can't find any information on this now). Is that correct and, if so, why?


Edit:

Here's an example that clarifies what I mean by "enum value" above. I mean taking the address of first_value below, not taking the address of an actual instance of an enum:

enum myenum
{
    first_value,
    second_value
};
+15  A: 

if you have:

enum E {
    X, Y, Z
};

then you cannot take the addresses of X, Y or Z, any more than you can take the addresses of 0, 1 or 2.

However, you can take the address of an enum instance:

typedef enum  {
   X, Y, Z
} E;

int main() {
  E e;
  E * ep;
  ep = & e;
}
anon
+4  A: 

The value names for enums are just constant aliases for integers, so it's meaningless to try to take their address. You can take the address of an enum variable just fine, of course.

chaos
The enum values are constants, but they are not macros. In most respects, they do of course work similarly to constants defined using macros, but they are different in some subtle ways -- they are subject to scoping, for example, whereas macros are not.
Martin B
They certainly are not #defines or macros. You may want to reword this.
anon
Yeah, Jesus, I sorely underestimated the pedantry I'd encounter. Mea maxima effing culpa. All better now.
chaos
I'm afraid pedantry (aka accuracy) is what SO is all about.
anon
While we're being pedantic, that is to say accurate, may we note that my original answer said they were macros *in effect*, which is an idiom commonly used to denote that a thing *is not* another thing but *acts* like it for some purpose under discussion?
chaos
Well, they are not macros, in effect. If they were, you could do things you do with macros, like #undef them. If you had said they were integers in effect, I would have no problem.
anon
Okay. I guess the difference between "in effect" and "for all possible purposes" isn't leaping out at anybody but me.
chaos
+15  A: 

"Enum value" is slightly ambiguous; however, I assume you mean the following:

enum myenum
{
    first_value,
    second_value
};

In this case, it is illegal to take the address of first_value. The reason for this is that first_value does not actually exist in memory anywhere... it is just a constant, effectively another name for the number 0 (of which, of course, you also cannot take the address).

If, on the other hand, you mean whether you can take the address of a variable that is declared as an enum:

enum myenum x;
enum myenum *mypointer=&x;

then that is definitely possible.

Martin B
Why the downvote?
Bastien Léonard
Everyone got one - part of the wonder of SO, I guess.
anon
Thanks! Yes, it was ambiguous. I clarified the question with the first example from your answer.
olovb
The important thing here, it seems, is that the integer does not exist in memory like a variable. Then it makes perfect sense to not be able to get its address.
olovb
A: 

As far as I remember enums in C are just integers. So it would be like pointing to local variable.

Pawel Lesnikowski
A pointer to a local variable is a perfectly valid thing in C.
anon
True, it's a long time since I've used C. There are some moments when I feel like I still know something, just like a PM that was a programmer 10 years ago...I should down vote for myself
Pawel Lesnikowski
+6  A: 

Enums are used to replace #define chains:

#define SUCCESS 0
#define LITTLE_ERROR 1
#define BIG_ERROR 2

This can be replaced with:

enum
{
    SUCCESS,
    LITTLE_ERROR,
    BIG_ERROR
};

An enum value such as SUCCESS is merely a symbol for an integer constant, which won't be stored anywhere in the program's memory. Thus it doesn't make sense to take its address.

Bastien Léonard
+1  A: 

pls refer the example below:

enum { FIRST,SECOND }var;

here the FIRST and SECOND are constant macros.Hence '&' operator will not work, since it is not stored anywhere in the memory

But for 'var' a memory location will be assigned and the '&' operator can be used.

Tony