I'm working on a project for school:
I have to create a function with this prototype:
int ddg_add_player(ddg_t *ddg, player_t *player);
Here is ddg_t
struct :
typedef struct
{
int day; /**< The DDG date (day). */
char *dmname; /**< The dungeon master name. */
int month; /**< The DDG date (month). */
char *name; /**< The DDG name. */
int nplayers; /**< The DDG number of players. */
player_t **players; /**< The DDG players. */
int year; /**< The DDG date (year). */
} ddg_t;
So I have to add filled player_t
structures to a ddg_t
structure. And I don't know how to process,
I tried with
ddg->players->ac = player->ac;
But it's obviously not working.
Just for this example let's say player_t
is as follows:
typedef struct
{
int ac; /**< The player armor class. */
} player_t;
I don't know why I have to use this player_t **players
and I can't ask my teacher since I'll be evaluated on this project.
On another note, I do have an array of structure player_t
, since I have multiple players.
Initially I thought I had to pass one structure at the time and my call looked like this:
for(i = 0; i < nPlayers; i++) {
ddg_add_player(&ddg, &players[i]);
}
My call now looks like this:
ddg_add_player(&ddg, players); /* players is my array of struct */
and the function like this :
int ddg_add_player(ddg_t *ddg, player_t *player){
ddg->players = malloc(sizeof(player_t) * 3); /* i have 3 players */
ddg->players[0]->ac = player[0].ac;
printf("%d\n", ddg->players[0]->ac); /* just to check */
return 0;
}
So, the code works, but in the header given by the teacher it says I have to add one player at the time.
Is there a way to do that?
I tried your solution @chmike but it gives me this error :
error: expected expression before ‘player_t’
oc(ddg->players, (ddg->nplayers+1)*sizeof(*player_t));
^~~~~~~~
I tried to remove the *
but then it gives me a segmentation fault.