0

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.

stev
  • 83
  • 1
  • 8
  • You have to specify a size for the array or declare `str` as a `char*` depending on what you intend to do with the structure. C++ doesent magically handle and resize the size of an array. – Tudor Mar 30 '20 at 20:41
  • C++ doesn't have flexible array members. https://stackoverflow.com/questions/4412749/are-flexible-array-members-valid-in-c – Barmar Mar 30 '20 at 20:43
  • And in C, the flexible array member is not included in `sizeof()`, since that's calculated at compile time. – Barmar Mar 30 '20 at 20:43
  • I don't think you can initialize a FAM like that, either. – Barmar Mar 30 '20 at 20:44
  • Off-topic as C++, since C++ does not support flexible array members – Peter Mar 30 '20 at 20:47
  • Does this answer your question? [Are flexible array members valid in C++?](https://stackoverflow.com/questions/4412749/are-flexible-array-members-valid-in-c) – Peter Mar 30 '20 at 20:47
  • OK, but how can I then initialize my_struct2 with the value of my_struct size? – stev Mar 30 '20 at 20:51
  • @Peter, that does not answers my question. – stev Mar 30 '20 at 20:54
  • I understand that `sizeof()` is calculated at compile time. That is why the `sizeof()` my date string is correct. However, the macro initialization seems to disturb the compiler. As the compiler knows the format of `my_struct_t` and has all data to build `my_struct` I expect that it can also determine at compile time the size of it. But obviously it is not the case. How can I initialize `my_struct2` with the correct value for `my_struct` size? – stev Mar 30 '20 at 21:01
  • @stev - In context of C++ (which your question was tagged before someone removed those tags) the correct answer is that you are using an invalid construct. – Peter Mar 30 '20 at 22:54
  • The title clearly states C (not C++) as being the context. – stev Mar 31 '20 at 10:15

0 Answers0