as joachim Pileborg already mentioned, if you have a c++11 compatible compiler it is possible to pass an literal array to a function, and the function (Ethernet.begin) has a begin(std::initializer_list)
variant.
however, your example is close to a solution. make mac
global (const if possible) and remove the define:
uint8_t const mac[] = { 0x43, 0xA3, 0xDA, 0x0D, 0xF5, 0xA5 };
//^^^^^ -> if begin isn't able to handle const, remove the const here.
void setup() {
if (Ethernet.begin(mac) == 0) {
...
}
}
this will instatiate mac only once and reuse it (no memory issue).
there are some possibilities, depending on the code:
Ethernet.begin
is defined with a reference/pointer as parameter. this would mean your are passing just a reference which is ussaly smaller then 6 byte (sizeof(mac)). and mac is just once stored in memory.
here mac have to be declared const. so the compiler could optimize it and incorporating the value directly into the machines instruction opcodes. therefore you have to compile with optimization-flags on. see here
if both are not a case, then it is still ok, because mac is instantinated still once and will be passed by value (copy) over stack memory.
the best is both... byte const mac
and begin(byte const*)
, so the compiler would decide the best way to spare memory and time.
there is no disadvantage to make it global. a define will result in translated opcode, also maybe global const will (see above case 2) if optimized. but a const will result in one instance also one opcoder (while initializing mac) and the rest in references/aliases to this instance (addressing the value). the behaviour is defined by the implementation of your Ethernet
.