Initialize sub-struct array inside main-struct

Hi,

I'm working on developing data structure for glcd menu-list.

I've concluded to this working code, I've tested it on codeblocks:

#include <iostream>

using namespace std;

typedef struct {
  const char* sub_name;
  uint8_t     x;
  uint8_t     y;
}sub_struct;

typedef struct {
  const char*   main_name;
  uint8_t       index;
  sub_struct    sub_struct_list[];
}main_struct;

main_struct strct1 = {"main_name", 1, {"sub_name1", 8, 9, "sub_name2", 88, 99}};
main_struct *pg_ptr;
int main()
{
    pg_ptr = &strct1;
    cout << +pg_ptr->sub_struct_list[0].sub_name << endl;
    cout << +pg_ptr->sub_struct_list[1].sub_name << endl;

    return 0;
}

My question is about:

sub_struct sub_struct_list[];, is it a good solution for my task ?

Have you tested it and does it work?

1 Like

Yes it works.

But my question, how does that array work ?

The array works inside of the main_struct.
And you can have an array of main_structs, each with its own array of sub_structs.

It's just a more organized way to have arrays but they are all arrays like any other.

1 Like