1

Is it possible to use a name instead of a number to address an array? I was reading about enum lists and thought it might be possible. ie int array[3] = {something, somethingElse, somethingMore]

array[second] => somethingElse

I am asking as I have an array looking after flags for several functions and would like to be able to use the function name instead of a number to address the array.

bool flag[6] = {1, 1, 1, 1, 1, 0}; // Power, Jump 3, Jump 7, Fade 3, Fade 7, Pause active
  • 2
    I'm voting to close this question as off-topic because it's a generic programming question. – Enric Blanco May 10 '17 at 21:32
3

No, but you can use a struct:

struct flags {
    uint8_t power;
    uint8_t jump3;
    uint8_t jump7;
    .... etc ....
};

struct flags myFlags;

myFlags.power = 1;
if (myFlags.jump3) {
    .... stuff ....
}

Or you can continue to use a simple numeric array and assign names to the numbers, either with #define, const variables, or an enum.

enum flags {
    power, jump3, jump7, fade, fade7, pause
};

bool flag[6] = {1, 1, 1, 1, 1, 0};

flag[power] = 1;
if (flag[jump3]) {
    .... whatever ....
}
  • That's brilliant thanks, ill give them a go. – Matt May 10 '17 at 17:47
  • Used the struct method, works perfectly :) thanks – Matt May 10 '17 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.