Reputation: 25
In my code I got a struct of this kind and two functions:
typedef struct {
char team;
int score;
} Player;
myfunc1 (Player *players) {
players->score = 105;
myfunc(?);
}
myfunc2(?) {
//change again points and team character
}
And in main I create an array of that struct and pass it to a function:
int main () {
Player players[2]
myfunc1(players)
}
I get to work the first function, but I don't know what argument I should pass from the first to the second to modify the array of players[2] created in main.
Upvotes: 0
Views: 51
Reputation: 5289
You can again use a simple pointer to access the data from players
:
void myfunc2 (Player *player)
{
players->score = 123;
}
Call it from your myfunc1
like this:
myfunc2(players);
You will actually pass an address to Player structure stored in pointer Player* players
(in function myfunc1
) into local pointer variable Player *player
in function myfunc2
.
To modify players[1] in your main
function, call myfunc1
like this:
int main () {
Player players[2]
myfunc1(&players[1]); // & = give an address to your struct
}
Be careful about array indexes, they do start from zero, so if yu have an array with capacity of two (Player players[2]
) then there are only two valid indexes: 0
an 1
. If you access indexes beyound the capacity your code will sooner or later crash.
Upvotes: 3