用C枚举值的内存位置 [英] Memory location of enum value in C

查看:148
本文介绍了用C枚举值的内存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我读的地方,是非法采取枚举值的地址在C(不是左值枚举值;不过,我现在找不到这方面的消息)。这种说法是否正确,如果是这样,为什么?


编辑:

下面是一个例子来阐明我的意思是枚举值之上。我的意思是服用 FIRST_VALUE 下面的地址,而不是采取一个枚举的实际实例的地址:

 枚举myenum
{
    FIRST_VALUE,
    SECOND_VALUE
};


解决方案

枚举值是有些模棱两可;不过,我想你指的是以下内容:

 枚举myenum
{
    FIRST_VALUE,
    SECOND_VALUE
};

在这种情况下,它是违法的 FIRST_VALUE 的地址。这样做的原因是, FIRST_VALUE 实际上并不存在于存储器中的任何...它仅仅是一个常数,有效地别称数字0(当然,中,你也不能走地址)。

如果,在另一方面,你的意思是你是否可以采取被声明为枚举变量的地址:

 枚举myenum X;
枚举myenum * mypointer =安培; X;

那么这绝对是可能的。

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

解决方案

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

这篇关于用C枚举值的内存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆