1

I'm trying to create an array of strings, which is inside of a structure, and I'm having a bit trouble on the syntax part. This is my code:

typedef struct data_players {
    int id;
    int hp; //start = 20, Max = 30
    int wght; // Max = 20
    int atk;
    int def;
    char *inventory[20] = {
        inventory[0] = "knife";
        inventory[1] = "healthpack";
    }
} jogador;
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
Zetsuno
  • 97
  • 1
  • 9

1 Answers1

2

You can't assign inside struct definitions, nor are typedefs intended for instances; they are aliases for types. With something like:

typedef struct data_players
{
    int id;     
    int hp; //start = 20, Max = 30
    int wght; // Max = 20
    int atk;
    int def;
    char *inventory[20];
} data_players;

You can then do:

data_players jogador = {0, 20, 15, 5, 5, {"knife", "healthpack", /* ... */}};

With a designated initializer, you can do:

data_players jogador = {.inventory = {"knife", "healthpack", /* ... */}};
Aioi Yuuko
  • 118
  • 7
  • Just one thing, if instead of one player I got, an array of players, will player[i] = {.inventory = {"knife", "healthpack", /* ... */}}; work? – Zetsuno May 04 '15 at 21:04