Creating structs on the fly [SOLVED]

Hi,

I have a struct that contains another struct

typedef struct
{
  uint8_t r = 0;
  uint8_t g = 0;
  uint8_t b = 0;
} Color;

typedef struct
{
  uint8_t numLeds;
  uint16_t *positions;
  Color brightColor;
  Color dimColor;
} Section;

I want to fill the 'Section' now

const Section sect1 = {
  sect1size,  // Exists
  sect1pos,  // Exists
  { 128, 0, 10 },
  { 23, 0, 2 }
};

, but Arduino doesn't like the way I want to create those Colors 'on the fly'. How do I actually do that?

Daniel

What data type are sect1size and sect1pos declared as? Can you show us the declaration please?

aarg:
What data type are sect1size and sect1pos declared as? Can you show us the declaration please?

const uint8_t sect1size = 50;
const uint16_t sect1pos[sect1size] = { 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280,
                                       129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144
                                     };

sect1pos is passed as reference.

Daniel

You don't typedef structs. Here's an example on how to create a struct propperly:

struct exampleStruct {
  char z;
  float y;
} testStruct;

Also, when dynamically creating structs, it's probably best to create it and initialize its members after creation. i.e.:

exampleStruct anotherTestStruct;
anotherTestStruct.z = 'h';
anotherTestStruct.y = 4.5;

When your struct has a default member initializer, it cannot be aggregated initializing anymore.

typedef struct
{
  uint8_t r;
  uint8_t g;
  uint8_t b;
} Color;

arduino_new:
When your struct has a default member initializer, it cannot be aggregated initializing anymore.

Thanks a lot, solved!