Quick question about populating arrays

This is probably a silly question but I can't seem to find the answer.......

typedef struct {
  byte  a;
  byte  b;
} button_t;

button_t values[10];

Is it possible to populate the values in one of the array elements like this:

values[].a = (1,2,3,4,5......)

I know this doesn't work but hopefully you will get what I am trying to do.

Or do I need to do this:

values[0].a = 1;
values[1].a = 2;
values[2].a = 3;
.
.
.
.

Thanks in advance.

You can. You need to enclose all the elements for a structure within curly braces and separate these groups by comms.. Essentially each array element is a group of variables instead of the individual items if a simple array.

struct_type data[3] = {{1,2,3},{3,4,5},{6,7,8}};

marco_c:
You can. You need to enclose all the elements for a structure within curly braces and separate these groups by comms.. Essentially each array element is a group of variables instead of the individual items if a simple array.

struct_type data[3] = {{1,2,3},{3,4,5},{6,7,8}};

So I can't just populate one of the elements?

Would this work

struct_type data[3] = {{1,,},{3,,},{6,,}};

So I can't just populate one of the elements?

Yes you can, if the element you want to populate is the first, just omit the second value.

You could put the data in flash then read it in at setup(), any way you want.

Otherwise, why not initialize all the values in one place to make checking and debug easier? If you don't know some, make them 0 or -1 to suit your program since they have to start with some value anyway?

If they're global or static, the compiler will initialise them the zero.

Yes. And if you want some initialized otherwise, you might as well do the lot in 1 place. It is easier to find when you need to check or to make changes.

Thanks to everyone for their assistance - I would have replied earlier but tonight has been my first chance to get back to my sketch.

This is what I have ended up with, and it works

typedef struct {
  byte xEEPROM;         // EEPROM adresss for stored value
  int  xPos;             // Stored value for X position
  byte yPos;            // Variable value for Y position
  int  yValue;          // Calculated % value of position x
  int  valuePos;        // X position for displaying the % value
} 
sliderButton_t;

sliderButton_t button[10] = { {0,50,0,0,0}, {0,84,0,0,0}, {0,118,0,0,0}, {0,152,0,0,0}, {0,186,0,0,0}, {0,220,0,0,0}, {0,254,0,0,0}, {0,288,0,0,0}, {0,322,0,0,0}, {0,356,0,0,0} };

If I did not insert the zero, i.e. used {,50,,,} I got an error message "LED_Controller:33: error: expected primary-expression before ',' token" so it would appear that you need to insert a value for each of the array elements, even if it is zero.

Cheers

If I did not insert the zero, i.e. used {,50,,,}

You should be able to write {0,50}

AWOL:

If I did not insert the zero, i.e. used {,50,,,}

You should be able to write {0,50}

You are correct, that does work XD

Thanks again...

If you swap two lines around in the struct you could just do:
sliderButton_t button[10] = { {50}, {84}, ... and so on.

  byte xEEPROM;         // EEPROM adresss for stored value
  int  xPos;             // Stored value for X position

Becomes:

  int  xPos;             // Stored value for X position
  byte xEEPROM;         // EEPROM adresss for stored value

Unless you are relying on the position of each item in the struct then the above would work fine.

UNO has 1k EEPROM....

GoForSmoke:
UNO has 1k EEPROM....

I am using a Mega but I am a bit confused about your comment - what does the amount of EEPROM have to do with anything??

Maybe it's a reference to the fact that you can't put a 10 bit address in an eight bit variable.
This may not be a problem if you never address single EEPROM bytes.

There are 256 4-byte words worth of space so you could use 8 bits to point to those.
Or use 8 bits if you wanted to make sure you never overfilled the EEPROM.
Not judging, just pointing out the address length, your app may take that into consideration....