I have the following 3 structs for a card game program
// Linked list of cards, used for draw & discard pile, players' hands
typedef struct cardStack {
struct cardStack *next;
Card *card;
} CardStack;
// A player and their hand
typedef struct player {
int playerNumber;
CardStack *hand;
} Player;
typedef struct _game {
CardStack *discardPile;
CardStack *drawPile;
Player *players[4];
int currentPlayer;
int currentTurn;
} *Game;
and this function to initialise the game struct
Game newGame(int deckSize, value values[], color colors[], suit suits[]) {
Game game = (Game)malloc(sizeof(Game));
game->players[0] = (Player*)malloc(sizeof(Player));
game->players[0] = &(Player){0, NULL};
game->players[1] = (Player*)malloc(sizeof(Player));
game->players[1] = &(Player){1, NULL};
game->players[2] = (Player*)malloc(sizeof(Player));
game->players[2] = &(Player){2, NULL};
game->players[3] = (Player*)malloc(sizeof(Player));
game->players[3] = &(Player){3, NULL};
for (int i = 1; i <= 7; i++) {
for (int j = 1; i <= NUM_PLAYERS; i++) {
Card card = newCard(values[i * j - 1], colors[i * j - 1], suits[i * j - 1]);
addToStack(card, game->players[j-1]->hand);
}
}
CardStack *discardPile = (CardStack*)malloc(sizeof(CardStack));
Card firstDiscard = newCard(values[28], colors[28], suits[28]);
addToStack(firstDiscard, discardPile);
game->discardPile = discardPile;
CardStack *drawPile = (CardStack*)malloc(sizeof(CardStack));
for (int i = 29; i < deckSize; i++) {
Card card = newCard(values[i], colors[i], suits[i]);
addToStack(card, drawPile);
}
game->drawPile = drawPile;
game->currentPlayer = 0;
game->currentTurn = 1;
return game;
}
It compiles fine, but when I try to run it, this line
game->players[0] = (Player*)malloc(sizeof(Player));
and similar, give an error "illegal array, pointer or other operation" I'm not sure whats wrong as I am just setting one pointer (in the array of pointers in the struct) to another
EDIT: unfortunately this is an assignment where the header file was given so I have no choice but to use the pointer typedef