14

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
};
James McNellis
  • 348,265
  • 75
  • 913
  • 977
olovb
  • 2,164
  • 1
  • 17
  • 20
  • 1
    That enum exists only in the memory of the compiler, not the executeable. You'll have to create an instance of the enum for it to appear in the executable. – Anonym Jan 13 '10 at 12:24

6 Answers6

28

"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
  • 23,670
  • 6
  • 53
  • 72
  • 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 Jul 30 '09 at 13:56
  • Are enums stored in ROM like const variables or are they stored in RAM? – AlphaGoku May 11 '16 at 05:08
18

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;
}
10

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
  • 60,478
  • 20
  • 78
  • 95
5

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
  • 122,029
  • 33
  • 303
  • 309
  • 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 Jul 30 '09 at 13:32
  • They certainly are not #defines or macros. You may want to reword this. –  Jul 30 '09 at 13:33
  • Yeah, Jesus, I sorely underestimated the pedantry I'd encounter. Mea maxima effing culpa. All better now. – chaos Jul 30 '09 at 13:35
  • I'm afraid pedantry (aka accuracy) is what SO is all about. –  Jul 30 '09 at 13:37
  • 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 Jul 30 '09 at 13:47
  • 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. –  Jul 30 '09 at 13:51
  • 1
    Okay. I guess the difference between "in effect" and "for all possible purposes" isn't leaping out at anybody but me. – chaos Jul 30 '09 at 13:57
  • @chaos if the there is only one memory location for all enumeration values, so where the values names are stored?? I mean if first_value means 0, second_value means 1 and .... hundred_value means 100, where those indication names are stored then in memory. specially for embedded program where we want to save code footprint – bouqbouq Oct 18 '15 at 12:43
  • 1
    @MakhloufGharbi They don't exist in the memory of the runtime program at all. They're compiler/preprocessor artifacts. – chaos Oct 19 '15 at 14:23
2

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.

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
Tony
  • 21
  • 2
0

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

Pawel Lesnikowski
  • 6,264
  • 4
  • 38
  • 42
  • 1
    A pointer to a local variable is a perfectly valid thing in C. –  Jul 30 '09 at 13:34
  • 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 Jul 30 '09 at 13:46