Why can't I assign a variable's address to an enum?
-
I am writing standard C programming language for an ARM processor and I would like to combine a handle (a pointer to a struct with state variables) with an enum like this:
struct MyStateStruct_t {
int a;
};const struct MyStateStruct_t myStates [2];
typedef enum
{
MY_SELECTOR_0 = (const int)(&myStates[0]),
MY_SELECTOR_1 = (const int)(&myStates[1])
} MyEnum_e;When I try to compile, I get the following errors: enumerator value for 'MY_SELECTOR_0' is not an integer constant enumerator value for 'MY_SELECTOR_1' is not an integer constant Does anybody know a workaround? Of course, I could use a look-up table array to convert from the enum-values to the addresses, but if possible I would like to avoid that. Thanks for any help!
-
I am writing standard C programming language for an ARM processor and I would like to combine a handle (a pointer to a struct with state variables) with an enum like this:
struct MyStateStruct_t {
int a;
};const struct MyStateStruct_t myStates [2];
typedef enum
{
MY_SELECTOR_0 = (const int)(&myStates[0]),
MY_SELECTOR_1 = (const int)(&myStates[1])
} MyEnum_e;When I try to compile, I get the following errors: enumerator value for 'MY_SELECTOR_0' is not an integer constant enumerator value for 'MY_SELECTOR_1' is not an integer constant Does anybody know a workaround? Of course, I could use a look-up table array to convert from the enum-values to the addresses, but if possible I would like to avoid that. Thanks for any help!
-
I am writing standard C programming language for an ARM processor and I would like to combine a handle (a pointer to a struct with state variables) with an enum like this:
struct MyStateStruct_t {
int a;
};const struct MyStateStruct_t myStates [2];
typedef enum
{
MY_SELECTOR_0 = (const int)(&myStates[0]),
MY_SELECTOR_1 = (const int)(&myStates[1])
} MyEnum_e;When I try to compile, I get the following errors: enumerator value for 'MY_SELECTOR_0' is not an integer constant enumerator value for 'MY_SELECTOR_1' is not an integer constant Does anybody know a workaround? Of course, I could use a look-up table array to convert from the enum-values to the addresses, but if possible I would like to avoid that. Thanks for any help!
As Richard said enum is 1.) Restricted to a 16 bit integer constant 2.) Must be defined at compile time Your pointer is longer than 16 bits and the address is only available at runtime, so it double fails. You also haven't typedef the structure to a name. I suspect this is what you are trying to do
typedef struct MyStateStruct\_t { int a; } MYSTATE; // typedef the structure to a name typedef enum { MY\_SELECTOR\_0 = 0, MY\_SELECTOR\_1 = 1 } MyEnum\_e; const MYSTATE myStates\[2\]; // use structure name enum MyEnum\_e selector = MY\_SELECTOR\_0; // Set selector to 0 int result1 = myStates\[selector\].a; // result1 is myStates\[0\].a selector = MY\_SELECTOR\_1; // Set selector to 1 int result2 = myStates\[selector\].a; // result2 is myStates\[1\].a
In vino veritas