Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!
I'm working on an Arduino thing for work and I'm trying to make use of Structs to make things a little easier. Problem is, I don't know a lot about them! I also am a little fuzzy on pointers and referencing, so as you can imagine, this is tough for me.

So, I want to be a be able to pass a struct into a function and have it modify the data in the struct, either directly or returning the info. What's the best way to do that?

I have something along these lines:

code:
struct Player
{
uint8_t Button1_Pin;
uint8_t Button2_Pin;
uint8_t State;
uint8_t Score;
}

Player Player1; 
Player Player2;

and I'm thinking of trying to do something like this:

code:
void Read_Player(struct *ThePlayer)
{
if(digitalRead(ThePlayer->Button1_Pin) == LOW)
{
ThePlayer->Score++;
State = true;
}
if(digitalRead(ThePlayer->Button2_Pin) == LOW)
{
State = false;
}

}

void loop()
{
Read_Player(&Player1);
Read_Player(&Player2);
}
Now, I'm just kind of awkwardly thinking this out right now, I haven't even tried to compile, and I don't have a controller handy on me to see if it works... am I in the right direction? I imagine I'm missing something.

Adbot
ADBOT LOVES YOU

Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!
ah yeah forgot to do the membering there.

And I wouldn't have to do a return, correct? Because this manipulating the original data, rather than passing a copy.

  • Locked thread