Go Down

Topic: Quick question about populating arrays (Read 1 time) previous topic - next topic

Magicj

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

Code: [Select]
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.

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.

Code: [Select]

struct_type data[3] = {{1,2,3},{3,4,5},{6,7,8}};
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

Magicj


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.

Code: [Select]

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

Code: [Select]

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


AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GoForSmoke

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?

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

AWOL

If they're global or static, the compiler will initialise them the zero.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GoForSmoke

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.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Magicj

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

Code: [Select]
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

AWOL

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

You should be able to write
Code: [Select]
{0,50}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Magicj


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

You should be able to write
Code: [Select]
{0,50}


You are correct, that does work  XD

Thanks again...

Tom Carpenter

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

Code: [Select]

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

Becomes:
Code: [Select]

  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.
~Tom~

GoForSmoke

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Magicj


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??

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GoForSmoke

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....

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up