How to initialize Const Struct

Hello all. My first post as I am pulling my hair out, and not much left!

I am wanting to initialize a structure and store in ROM to save RAM resource. I’ve been trying different methods to accomplish this, but am not understanding the secret sauce.

I’ve attempted the following (and similar) as documented within C with no luck.

  typedef union  D12_t {
      struct {
        int     p1;
        unsigned char p2;
      } __attribute__((packed)) tlate[522];
      uint8_t stream[2610];
  } D12_t;

const static D12_t temp[] = {
  {0.[tlate.p1] = 0}
  {0.[tlate.p2] = 12}

  {1.[tlate.p1] = 23}
  {1.[tlate.p2] = 55}

  {2.[tlate.p1] = 33}
  {2.[tlate.p2] = 12}

};

I’m getting the following error message:
“expected Identifier before numeric constant”

Can some kind soul point me in the correct direction?

Thank you!

CG

You’d initialize like this

const static D12_t temp[] = { {0, 12}, {23, 55}, {33, 12} };
2 Likes

That didn’t work either.

The “temp” structure has 522 tlate elements "union"ed with 2610 “stream” elements. I don’t want multiple elements of the structure itself, I want to init the 522 tlate elements or I suppose the 2610 stream elements to known values. I did not see that distinction at first.

Once I remove the “figure out the size” portion of the definition, I now get “too many initializers for Const D12_t”

const static D12_t temp = {
{0x42082184, 0x10},
{0x42082184, 0x1e},
{0x43082184, 0xd0},
{0x43082184, 0xde}
};

The previous attempt uses what I thought was standard “C” syntax, however the Particle environment does not seem to like this and I need to figure out a solution. I supposes I could make this just a byte stream array and play games with casting to INT, but that seems lame to me.

Any other ideas?

Ok, found the secret sauce. This format was not intuitively obvious to me.

const static D12_t temp = {
{
{.p1 = 0x42082184, .p2 = 0x10 },
{.p1 = 0x42082184, .p2 = 0x1e }
}
};

I compared the values created by initialization within setup, versus the compile time values and are identical.

FWIW I had more than 500 values to initialize in this method.

Thank you all for listening!

CG

2 Likes