Hello, I’am having trouble with const data.
It should be easy, but…
(working with ESP32 on PlatformIO of course)
This is OK:
*main.cpp*
const char test[] = { 1,2,3 };
main() {
char buff[10];
strcpy(buff, (char *) test);
}
…but if the const data are on another file, linker returns Undefined Reference to test:
*main.cpp*
extern const char test[];
main() {
char buff[10];
strcpy(buff, (char *) test);
}
--------------------------
*file.cpp*
const char test[] = { 1,2,3 };
Thanks for help!
How is this fine? strcpy will look for a zero-terminated string, and the array you point it to has ano explicit '\0' character in it – so copying from it is undefined behavior, and it may copy data beyond the boundaries of the array.
Where is this file located?
both files are in /src directory
file.cpp must also contain an extern declaration of the variable.
Why? Is it a rule of C++ ?
I did it so many times with the standard C without that error…
Thanks!
Good question, I do not know this. Maybe try asking this at stackoverflow.com/.