Yes, but it is an incredibly convoluted way of doing it. This does the same thing (maps names to values) and doesn't use pointers (apart from the pointer to the constant string, which is the name of the item). No copying, no fiddling with taking addresses, etc.
float BatVolts = 13.5;
float ACVolts = 236;
float WindSpeed = 3;
float PVCurrent = 20;
// number of items in an array
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))
// structure for name/value mapping
typedef struct
{
const char * name;
float & val;
} xVar;
xVar vals [] = {
{ "BatVolts", BatVolts },
{ "ACVolts", ACVolts },
{ "WindSpeed", WindSpeed },
{ "PVCurrent", PVCurrent },
};
void showValues ()
{
for (int i = 0; i < NUMITEMS (vals); i++)
{
Serial.print (vals [i].name);
Serial.print (" = ");
Serial.println (vals [i].val);
}
} // end of showValues
void setup ()
{
Serial.begin (115200);
showValues ();
Serial.println ();
BatVolts = 12;
ACVolts = 240;
WindSpeed = 4;
PVCurrent = 18;
showValues ();
} // end of setup
void loop () {}
Output:
BatVolts = 13.50
ACVolts = 236.00
WindSpeed = 3.00
PVCurrent = 20.00
BatVolts = 12.00
ACVolts = 240.00
WindSpeed = 4.00
PVCurrent = 18.00
I haven't done looking up names, but if I can print them, I can look one up and return it. I'll leave that as an exercise. ![]()