0

I would like to create a constant structure in flash. This structure would consist of various sized elements including string arrays, shorts, bytes, etc. Strings should be null terminated, but may not entirely fill the array storage area allocated for it. The code shown below is what I have, but the compiler gives the following error.

../Source/flash_constants.h:1025:33: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
   const unsigned char Reg00[32] = "My String 1";

The const unsigned char Reg00[32] = "My String 1"; is a valid statement outside of the struct definition, but fails inside the struct definition.

Thanks.

struct
{
  const unsigned char Reg00[32] = "My String 1";
  const unsigned char Reg01[32] = "My String 2";
  const unsigned char Reg02[32] = "My String 3";
  const unsigned short Reg03 = 0;
  const unsigned short Reg04 = 0;
} ModBusIDReg;
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    First you define the type, and then you define a constant typed with it and initialized with desired values. – Eugene Sh. May 08 '18 at 18:53
  • Possible duplicate of [How to initialize a struct in accordance with C programming language standards](https://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-accordance-with-c-programming-language-standards) – MFisherKDX May 08 '18 at 18:56

2 Answers2

3

You can not do inline initialization of structures in C.

You need to define the structure, define the variable, and then initialize the variable:

struct
{
  const unsigned char Reg00[32];
  const unsigned char Reg01[32];
  const unsigned char Reg02[32];
  const unsigned short Reg03;
  const unsigned short Reg04;
} const ModBusIDReg = {
    "My String 1",
    "My String 2",
    "My String 3",
    0,
    0
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Initializing structure elements outside of the structure definition worked. Thanks ! However, this is a bit of a pain in the neck too. I have 100 of these elements to define, and now the initialization is somewhat disassociated with the definition (visually). For instance, the 53rd initializer is visually far away from the 53rd definition. I see lots of counting, and checking to make sure they line up. – Gregory Helton May 08 '18 at 19:27
  • @GregoryHelton Then using *designated initializers* as in [the answer by малин чекуров](https://stackoverflow.com/a/50240825/440558) could be a solution. Or consider a design where you don't need that many members. – Some programmer dude May 08 '18 at 19:37
3

You can also use the designated initializers(C99) which don't restrict you in initializing the members in fixed order:

struct
{
  const unsigned char Reg00[32];
  const unsigned char Reg01[32];
  const unsigned char Reg02[32];
  const unsigned short Reg03;
  const unsigned short Reg04;
} const ModBusIDReg = {

    .Reg01 = "My String 2",
    .Reg00 = "My String 1",
    .Reg02 = "My String 3",
};

Members that are not explicitly initialised are initialised as if the instance had static duration, since C has no partial initialization.