I need to hard code 8 byte addresses into char arrays of length 8. This must be done many places in my code (in function scope), so I have tried to come up with a one-liner. The following works perfectly in C when compiling with gcc.
char a[8];
void cpaddr(char target[], char *source) {
int i;
for (i=0; i<8; i++)
target[i] = source[i];
}
int main() {
char b[] = {0x00, 0x10, 0xFF, 0xCA, 0x00, 0x00, 0xA2, 0x7D};
cpaddr(a, b);
// line below does not compile with Arduino IDE
cpaddr(a, (char[]) {0x00, 0x10, 0xFF, 0xCA, 0x00, 0x00, 0xA2, 0x7D});
}
When compiling on Arduino the last line, which is the one-liner I was aiming for, does not compile. It gives:
/home/bob/Desktop/ate/Ate.ino: In function 'int main()':
Ate:101: error: taking address of temporary array
cpaddr(a, (char[]) {0x00, 0x10, 0xFF, 0xCA, 0x00, 0x00, 0xA2, 0x7D});
^
exit status 1
taking address of temporary array
What is the problem here, that apparently is not a problem with gcc?
How do I fix it?
( type[] ){val, ues}
is not a valid way to create a temporary in C++: stackoverflow.com/questions/15458883/… The solution is to typedef it.