Populated Structure declaration?

Hello everyone

My knowledge about structures is nothing so please bear with me. I'm busy trying to declare a structure but I'm getting the following error messages:

NRF24L01_Comms_Relay.ino:26:36: error: too many initializers for ‘int [0]’
NRF24L01_Comms_Relay.ino:32:36: error: too many initializers for ‘int [0]’

The error messages come up when I compile it with the following code:

typedef struct
 {
   int Transmission_Pathway[] = { 5,     //  Array Location:  0
                                  9,     //  Array Location:  1
                                  3,     //  Array Location:  2
                                  7,     //  Array Location:  3
                                  8,     //  Array Location:  4
                                  2,     //  Array Location:  5
                                  6};    //  Array Location:  6

   int Transmission_Info[]    = { 0,     //  Transmission_Node Address
                                  1,     //  Recipient_Node Address
                                  8,     //  Amount of HOPS
                                  0,     //  Previous_Node_Address  -  To be changed
                                  0};    //  Next_Node_Address      -  To be changed
 }
 
 Payload;

When I declare it as follows then it works fine:

 typedef struct
 {
   int Transmission_Pathway[7] = { 5,     //  Array Location:  0
                                  9,     //  Array Location:  1
                                  3,     //  Array Location:  2
                                  7,     //  Array Location:  3
                                  8,     //  Array Location:  4
                                  2,     //  Array Location:  5
                                  6};    //  Array Location:  6

   int Transmission_Info[5]    = { 0,     //  Transmission_Node Address
                                  1,     //  Recipient_Node Address
                                  8,     //  Amount of HOPS
                                  0,     //  Previous_Node_Address  -  To be changed
                                  0};    //  Next_Node_Address      -  To be changed
 }
 
 Payload;

The thing is that I might want to populate the arrays with more fields at a later stage so then I don't want to specify the amount of fields available in the array. Any suggestions for me?

Thanks everyone
Dirk

You can’t put initializers in a typedef, nor initialize a structure more than once (“more fields at a later stage.”)

I believe you can do that with a C++ class, which is more-or-less a “fancy structure.”

The behaviour is a feature/limit of C / C++ compilers; certainly all the ones I have used. The compiler is telling you it is not prepared to do all the work for you. In this case, calculating the size of the array and then the size of the structure and then initialising the array, would take a pass of the compiler too many.

There are many different ways around the limitation. Some are safer than others, some more efficient then others, some easier to maintain than others. For instance, you might put the length of the array in a variable or the entirety of the data in a macro.

Personally, for a fixed length array, I would go with what you already have, with the length of the array declared next to the initialiser making it easier to debug. However, if you need to start passing the array to functions, a variable becomes easier to maintain.

If what you are really after is a dynamically sized array, the answer is a bit more complicated.

I might want to populate the arrays with more fields at a later stage so then I don't want to specify the amount of fields available in the array

If you were able to increase the size of the array at a later stage then you would need the memory to be available to do it at that time so you might just as well declare the array at its maximum size to begin with.

Whilst I appreciate that your example is only that and not real code (or is it ?) why are the values in the array declared as ints rather than bytes if your aim is to save space ?

Hey guys

Thanks for all the replies and my apologies for the late reply. Thank you for the explanation as well. I will make use of a variable to size the arrays as suggested then. Much appreciated.

Regards
Dirk

UKHeliBob:
If you were able to increase the size of the array at a later stage then you would need the memory to be available to do it at that time so you might just as well declare the array at its maximum size to begin with.

One way, and I would recommend using C++ classes for this, would be to make objects that hold the array data with links or indexes in RAM to other objects. Newly allocated objects can link in with the others, even do sorted order.

There is an Arduino bootloader that allows sketches to write to flash, potentially wiping your sketch out if you get the address wrong. You can store constant data known at compile time in flash using PROGMEM to save using RAM. That bootloader allows writing to flash at run time.

OP, a struct is a definition of data by compiler name, data type and address relative to the struct base address. It’s a template of a data type. You can make a pointer to your type struct and pointers to other structs and use one common buffer… like the default 512 byte SD buffer that gets SD reads and does SD writes.

You can write what is in the buffer to SD and you can read from SD to the buffer, changing the arrays.
You can log your data by writing the buffer occasionally, put in debug data instead of printing to serial monitor.

Look into SD and Arduino, You can make your own if you can solder or get a cheap module if not.