Go Down

Topic: Quick question about populating arrays (Read 2674 times)previous topic - next topic

Magicj

Dec 21, 2012, 10:34 am
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;
.
.
.
.

marco_c

#1
Dec 21, 2012, 10:55 am
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
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

Magicj

#2
Dec 21, 2012, 12:11 pm

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

#3
Dec 21, 2012, 12:41 pm
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

#4
Dec 21, 2012, 12:45 pm
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?

Nick Gammon on multitasking Arduinos:
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

AWOL

#5
Dec 21, 2012, 12:47 pm
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

#6
Dec 21, 2012, 08:11 pm
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.
Nick Gammon on multitasking Arduinos:
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Magicj

#7
Dec 27, 2012, 01:28 pm
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

#8
Dec 27, 2012, 01:34 pm
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

#9
Dec 27, 2012, 10:53 pm

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

#10
Dec 27, 2012, 11:48 pm
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

#11
Dec 28, 2012, 04:14 am
UNO has 1k EEPROM....
Nick Gammon on multitasking Arduinos:
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Magicj

#12
Dec 28, 2012, 11:23 am

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

#13
Dec 28, 2012, 11:30 am
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

#14
Dec 28, 2012, 01:14 pm
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....

Nick Gammon on multitasking Arduinos:
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up

Please enter a valid email to subscribe