Pages: [1]   Go Down
Author Topic: Quick question about populating arrays  (Read 1198 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
struct_type data[3] = {{1,2,3},{3,4,5},{6,7,8}};
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Full Member
***
Karma: 3
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
struct_type data[3] = {{1,,},{3,,},{6,,}};
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4816
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4816
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Full Member
***
Karma: 3
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I did not insert the zero, i.e. used {,50,,,}
You should be able to write
Code:
{0,50}
Logged

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

Offline Offline
Full Member
***
Karma: 3
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I did not insert the zero, i.e. used {,50,,,}
You should be able to write
Code:
{0,50}

You are correct, that does work  smiley-lol

Thanks again...
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  byte xEEPROM;         // EEPROM adresss for stored value
  int  xPos;             // Stored value for X position
Becomes:
Code:
  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.
Logged

~Tom~

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4816
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

UNO has 1k EEPROM....
Logged

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

Offline Offline
Full Member
***
Karma: 3
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4816
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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

Pages: [1]   Go Up
Jump to: