I am getting a wrong size of a struct containing a const char array.
Example (Arduino code):
#include <Streaming.h>
struct my_struct_t {
uint8_t len;
const char str[];
};
// Macro to init struct from string
#define MAKE_STRUCT(s) { .len = sizeof(s), {.str = s} }
#define my_str "30-03-2020"
const struct my_struct_t my_struct = MAKE_STRUCT(my_str);
struct my_struct2_t {
uint8_t index;
uint8_t size;
};
const my_struct2_t my_struct2 = {0, sizeof(my_struct)};
void setup() {
// put your setup code here, to run once:
while(!Serial); delay(10);
Serial << ("sizeof(my_str) = ") << sizeof(my_str) << endl;
Serial << ("my_struct.len = ") << my_struct.len << endl;
Serial << ("sizeof(my_struct) = ") << sizeof(my_struct) << endl;
Serial << ("my_struct2.size = ") << (my_struct2.size) << endl;
}
void loop() {
}
The values printed in the serial window:
sizeof(my_str) = 11
my_struct.len = 11
sizeof(my_struct) = 1
my_struct2.size = 1
The problem is that size of my_struct is wrong (expected is 1+11=12) and this way my_struct2 will be initialized with a wrong value.
What am I doing wrong?
How can I get the correct size value of my_struct
to be used in my_struct2
?
EDIT
Just want to mention that my_struct
is correctly allocated in the flash.