Hi, I'm currently learning about structures and I would like to know what the difference is between initialising a structure like this:
1 2 3 4 5 6 7 8 9
|
struct player hero, enemy;
hero.health = 100;
hero.attack = 10;
hero.defense = 10;
enemy.health = 100;
enemy.attack = 10;
enemy.defense = 10;
|
...and this:
1 2 3 4 5 6 7 8 9
|
struct player hero =
{
100, 10, 10
};
struct player enemy =
{
100, 10, 10
};
|
Is one better than the other for any particular purpose, or is it just an aesthetically preferential thing?
I would also like to know, is this correct implementation of typedef using structs?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
struct player
{
int health;
int attack;
int defense;
};
typedef struct player Player;
int main()
{
Player hero =
{
100, 10, 10
};
Player enemy =
{
100, 10, 10
};
}
|
It compiles fine, so I think I've done it right, but I'd like to be sure.
Also, is the sole purpose of using typedef with structures, or variables in general, simply to reduce 'clutter' on the screen?
As I said, I'm new to structures, so I'd just like a few things clarified. Thank you very much!