Does anyone know if it's possible to name indexes for arrays? For example:
int myArray[2];
myArray['Bob'] = 1;
myArray['Fred'] = 2;
Instead of traditionally using numbers for the array indexes? This is possible in other languages but I can't see to get it with the Arduino or find references to it.
Bob and Fred are multi character constants, so whilst legal, their values are quite high, and probably off the end of RAM.
What is it you are trying to do?
monkey123:
Does anyone know if it's possible to name indexes for arrays? For example:
int myArray[2];
myArray['Bob'] = 1;
myArray['Fred'] = 2;
Instead of traditionally using numbers for the array indexes? This is possible in other languages but I can't see to get it with the Arduino or find references to it.
No, it's not. Probably you want to use constants or #defined(d) symbols:
I think that they are often overlooked, and complex solutions invented to accomplish the same functionality.
Don't forget the enum is not limited to integer values, nor linear progressions, nor does it need to start at 0, though those are all default properties.
enum Color {Red=-6, Green=9, Blue=4};
is a valid enum declaration (although useless in this instance).
enums can make your code more readable and in some cases, errors a bit harder to make.
IIUC the Arduino makes a memory copy of every constant, literal, etc, in your code and I don't know if it compiles to the smallest size or the size of a variable it involves with or what. To play it safe, I declare variables in the size I want. It's not like I have RAM to waste.