#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2015
    Posts
    11
    Rep Power
    0

    Question how to access a list of varibles as an array


    I need to access this list of variables as an array
    and still access them by name only
    how can it be done ??

    uint8_t SN1;
    uint8_t SN2;
    uint8_t SN3;
    uint8_t SN4;
    uint8_t SN5;
    uint8_t SN6;
    uint8_t SN7;
    uint8_t SN8;
    uint8_t TX_set;
    uint8_t CH1_gain_set;
    uint8_t CH2_gain_set;
    uint8_t P_PH1_set;
    uint8_t P_PH2_set;
    uint8_t A_PH1_set;
    uint8_t A_PH2_set;
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,435
    Rep Power
    9645
    Don't.

    What is your situation where you think you need to do this?
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    Or use an associative array. gawk is a really stupendous language.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2015
    Posts
    11
    Rep Power
    0
    Originally Posted by requinix
    Don't.

    What is your situation where you think you need to do this?
    short of doing
    uint8_t EE_shadow_buff[32];

    and changing every variable to EE_shadow_buff[0]; // SN1
    EE_shadow_buff[1]; // SN2
    EE_shadow_buff[2]; // SN3 and so on...

    would be a lot work as they are used lots of places and would be unclear

    #define SN1 EE_shadow_buff[0] // does not work

    is there some kind of structure or union I can use ???
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    Use an enum perhaps
    Code:
    #include <stdint.h>
    
    typedef enum {SN1, SN2, SN3, SN4, SN5, SN6, SN7, SN8,
        TX_Set, CH1_gain_set, CH2_gain_set, 
        P_PH1_set, P_PH2_set,
        A_PH1_set, A_PH2_set,
        /* Add more as needed */
        MAX_ITEMS
    } SwitchStates;
    
    int main(void) {
       uint8_t EE_shadow_buf[MAX_ITEMS];
    ...
    ...
       EE_shadow_buf[SN3] = 47;    /* Write to item */
       foo = EE_shadow_buf[SN5];   /* read from item */
    ...
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo