multidimensional array within a structure

Hi there,

I am trying to assign a two-dimensional array located in a structure and I assumed it should work like this:

        struct data_struct
            int array[][2];

        data_struct data;

        data.array[][2] = {{0, 0}, {0, 0}, {0, 0}};

but I get an error:

expected primary-expression before ']' token

I tried different sytaxes with different errors, but can't get it right. Can anyone please tell me how I can do this?

int array[][2];

You can't do that you must provide both dimensions or an initializer list.

data.array[][2] = {{0, 0}, {0, 0}, {0, 0}};

You can't use an initializer list here. It's one element at a time after the declaration.



        struct data_struct
            int array[3][2]= {{0, 0}, {0, 0}, {0, 0}};


        data_struct data;

You should be able to leave the first rank blank, but the gcc compiler won't allow that. I'm nit sure why.

Oh, I see. So I would have to to waste SRAM by using fixed arrays! I was hoping I could dinamically change them in runtime:( Need to think of a different approach then...



Could you describe what you're trying accomplish a bit more?

I’m planning a control for a bread baking machine and the recipes should be entered/edited via LCD-menu.

So I need a variable structure of values!

Say an array of structures (recipes) that include an [n x 3]-array of integers, where n is the quantity of machine actions (knead, dough, bake, …) and 3 is some data (like index, duration, temperature).

If I would use fixed arrays I’d have to fix the quantity of possible recipes as well as the quantity of possible actions within each recipe. Since some recipes include fewer actions then others, this is not a good option (give away of SRAM).

Any hints how this can be realized dynamically in Arduino?

Assuming the recipes don't change, they don't need (or is that "knead"?) to go anywhere near RAM.

They will be saved on a SD card, but I need to load them for editing purposes (and to run them of course). Well at least ONE has to be loaded (so loading just ONE at a time might be the better solution any way!)


You can always put in a pointer to the array and then use malloc() to get the memory at runtime, but that is problematic on the RAM-Limited arduino due to memory fragmentation.

The main thing to be aware of is how very little RAM is available - the Uno has 2k bytes and
the Mega 8k. This also is used for the stack so you really get very little.

Hm, can you put the array in EEPROM? you probably don't need to access it that quickly.

is the data time related? compress it into bytes

looks like you have plenty of storage for a lot of Recipes, no?

struct Recipe{
  byte kneadMins;
  byte proofMins;
  byte preheatMins;
  byte bakeMins;

const Recipe pumpernickel = {5, 25, 8, 40};
const Recipe wholeWheat   = {3, 18, 8, 35};
const Recipe crustyFrench = {3, 22, 8, 33};
Recipe Custom1 = {3, 22, 8, 33};
Recipe Custom2 = {3, 22, 8, 33};

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly: