"Static struct" or How do I store multiple values in a group?

Hi,

I am working on a project in which I split an LED strip up into sections. Every section has

  • An array containing the LED indeces
  • An Integer telling the code how long the array is
  • Some more stuff, not important for this question

I would like to save all those values in one 'group' - comparable to a struct - that I can just pass to my functions. However, just creating a typedef struct 'Section' won't work because the arrays are a different size for each section. What I would want:

struct sect1
{
   const uint8_t length;
   uint16_t positions[length];
   // stuff
};

Anyone got any ideas? I don't know how well I explained it, feel free to ask.

Put a pointer to the array in the struct instead. Declare the arrays separately and initialize the pointers to point to them in setup.

You could use a template:

[color=#5e6d03]template[/color] [color=#434f54]<[/color][b][color=#d35400]size_t[/color][/b] [color=#000000]N[/color][color=#434f54]>[/color]
[color=#00979c]struct[/color] [color=#000000]section[/color] [color=#000000]{[/color]
    [color=#000000]constexpr[/color] [color=#00979c]static[/color] [b][color=#d35400]size_t[/color][/b] [color=#d35400]length[/color] [color=#434f54]=[/color] [color=#000000]N[/color][color=#000000];[/color]
    [color=#00979c]uint16_t[/color] [color=#000000]positions[/color][color=#000000][[/color][color=#000000]N[/color][color=#000000]][/color][color=#000000];[/color]
    [color=#00979c]const[/color] [color=#00979c]char[/color] [color=#434f54]*[/color][color=#000000]stuff[/color][color=#000000];[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#000000]section[/color][color=#434f54]<[/color][color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]s[/color] [color=#434f54]=[/color] [color=#000000]{[/color]
  [color=#000000]{[/color][color=#000000]1[/color][color=#434f54],[/color] [color=#000000]2[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#000000]}[/color][color=#434f54],[/color]
  [color=#005c5f]"stuff"[/color][color=#434f54],[/color]
[color=#000000]}[/color][color=#000000];[/color]

Otherwise, you'll either have to save the arrays elsewhere and save them by pointer, or you'll have to use dynamic memory allocation (use an std::vector if possible).

Pieter

PieterP:
You could use a template:

[color=#5e6d03]template[/color] [color=#434f54]<[/color][b][color=#d35400]size_t[/color][/b] [color=#000000]N[/color][color=#434f54]>[/color]

struct section {
    constexpr static size_t length = N;
    uint16_t positions[N];
    const char *stuff;
};

section<3> s = {
  {1, 2, 3},
  "stuff",
};

That sounds quite interesting. Too bad I have no clue what a template is. Any resources? A quick google search only brought up people that are at least as confused as I am.

Daniel

Try cplusplus.com.

A downside I see to the template approach is that structs with different sized arrays will be different types. So, you can't put them in an array.

I suppose you could define them all from a base class (struct) and refer to them via an array of pointers to the base (polymorphism).