简体   繁体   中英

Using a variable to declare multiple structs in C

I am wondering if there is a way that I can declare x amount of structs using the i variable from a for loop:

int playersAmount, i;

printf("How many players are there?");
scanf("%i", &playersAmount);

for (i = 1; i <= playersAmount; i++) {
    struct players //player(i);
}

Let's say I would like there to be 5 players. I would like the for loop to create struct players player1, struct players player2, struct players player 3, etc... based on the i variable.

Thanks!!!

you can create an array of struct like

struct players myplayers[5];

and then using the for loop you can assign values like

int main()
{
int a;
 for(a = 0; a < 5; a++)
 {
        myplayers[a].score = 1;
 }

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM