Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why can't I assign a variable's address to an enum?

Why can't I assign a variable's address to an enum?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestionlearning
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    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!

    L L 2 Replies Last reply
    0
    • A arnold_w

      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!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Enums must be defined integer constants at compile time.

      1 Reply Last reply
      0
      • A arnold_w

        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!

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups