Creating an array of variable names

Hi, I am trying to create an array out of these names, is that possible?

DeviceAddress is part of the DallasTemperature library

DeviceAddress black = {0x28, 0xFF, 0x1A, 0xAA, 0x62, 0x15, 0x03, 0x20};
DeviceAddress red   = {0x28, 0xFF, 0xA9, 0xB4, 0x62, 0x15, 0x03, 0x0C}; 
DeviceAddress green = {0x28, 0xFF, 0xF5, 0xB3, 0x62, 0x15, 0x03, 0x3E};

How would I make this work?
DeviceAddress devices[] = {black, red, green};

So that I can use;
for(DeviceAddress device : devices) {
  sensors.setResolution(device, 10);
}

Thanks, Thorin

You can't put variable names in an array because the names are only used by the compiler and do not exist in the code that is uploaded to the Arduino.

You can create an array of pointers, but that may be a little too complex if you are a beginner.

A simpler way would be to make an array that holds the addresses - something like

byte DeviceAddresses[3][] = {
           {0x28, 0xFF, 0x1A, 0xAA, 0x62, 0x15, 0x03, 0x20}.
           {0x28, 0xFF, 0xA9, 0xB4, 0x62, 0x15, 0x03, 0x0C}.
           {0x28, 0xFF, 0xF5, 0xB3, 0x62, 0x15, 0x03, 0x3E}
       }

(hope I have the syntax correct - not quite, see Reply #2) and then you can refer to them as DeviceAddresss[0] or [1] etc.

You could also have

#define black 0
#define red 1
#define green 2

and then you could refer to the addresses as

DeviceAddresses[red];

etc

...R

DeviceAddress devices[] = {
  {0x28, 0xFF, 0x1A, 0xAA, 0x62, 0x15, 0x03, 0x20},
  {0x28, 0xFF, 0xA9, 0xB4, 0x62, 0x15, 0x03, 0x0C},
  {0x28, 0xFF, 0xF5, 0xB3, 0x62, 0x15, 0x03, 0x3E}
};

Then, use the #define stuff Robin2 suggested, to be able to select devices[ red ]. Or, use i from 0 to 2 in a for loop, to operate on each device.

Although a red thermostat doesn't make much sense...

You need to get straight the difference between a variable (which has an address in memory), its name (compile time only), and its value.

At compile time there is a mapping from name to address that the compiler knows about and sorts out for you so you don't have to see bare addresses. At runtime you mostly are interested in the value stored in a variable, not the variable itself (unless using pointers or pass by reference).

Since you’ve only provided a code fragment, you won’t get a very good answer…

DeviceAddress black = {0x28, 0xFF, 0x1A, 0xAA, 0x62, 0x15, 0x03, 0x20};
DeviceAddress red   = {0x28, 0xFF, 0xA9, 0xB4, 0x62, 0x15, 0x03, 0x0C}; 
DeviceAddress green = {0x28, 0xFF, 0xF5, 0xB3, 0x62, 0x15, 0x03, 0x3E};

DeviceAddress *devices[] = {black, red, green};

for(int i=0; i<sizeof(devices) / sizeof(DeviceAddress); i++) {
    sensors.setResolution(devices[i], 10);
}

devices is a pointer to one of the DeviceAddress objects which can be used to pass the address to the setResoution function, assuming it is written to accept such an argument…
Regards,
Ray L.

for(int i=0; i<sizeof(devices) / sizeof(DeviceAddress); i++)Close…